Jade Bell
Jade Bell

Reputation: 41

Python: How do you print out "Correct!" instead?

 # These lines print out the question, possible answers and a prompt for the user to input
  a string

 print("ART AND LITERATURE: Who painted Starry Night?")
 print("a. Vincent Van Gogh")
 print("b. Michelangelo")
 print("c. Leonardo Da Vinci")
 answer = print(input("Enter your choice:"))
 
# Semantic error: prints out 'a' and 'The correct answer was a' when input is 'a'
# Desired output: "Correct!" when input is 'a' and 'The correct answer was a' for other 
  inputs

 if answer == 'a':
    print("Correct!")

 else:
    print("The correct answer was a")

The first block of code prints out the question, possible answers, and a prompt for the user to input a string.

The if-else statement has a semantic error as it prints out "a" and "The correct answer was a" even when the input is indeed, "a".

How do I fix this so that "Correct!" is printed when the input is 'a' and 'The correct answer was a' for other inputs?

Upvotes: 0

Views: 120

Answers (3)

Gavin Wong
Gavin Wong

Reputation: 1260

You do not need to use print when using input.

Hence, just execute:

print("ART AND LITERATURE: Who painted Starry Night?")
print("a. Vincent Van Gogh")
print("b. Michelangelo")
print("c. Leonardo Da Vinci")
answer = input("Enter your choice:")

if answer == 'a':
    print("Correct!")

else:
    print("The correct answer was a")

OUTPUT:

ART AND LITERATURE: Who painted Starry Night?
a. Vincent Van Gogh
b. Michelangelo
c. Leonardo Da Vinci
Enter your choice:a
Correct!

Upvotes: 1

ILoID
ILoID

Reputation: 93

answer = print(input("Enter your choice:"))

You assigned a print statement to your answer variable, so when this line is executed, it prints what the user inputs too. Quick fix:

answer = input("Enter your choice:")

Upvotes: 0

wasif
wasif

Reputation: 15488

Change answer = print(input("Enter your choice:")) to answer = input("Enter your choice:") because the first one just prints the value not to the variable.

Upvotes: 0

Related Questions