Python Forum

Full Version: Coding error?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Have been helping granddaughter to get started with Python and she has written a little program (below) which looks good but must have a flaw somewhere as final answer is always zero. Can anyone see where the error is? Thanks.

name = input("Hello, what is your name?")
print(name, ",welcome to the car quiz!")

first_answer = 2
score = 0
input("Q1 - Which country does Porsche come from?")
input("1 - England")
input("2 - Germany")
input("3 - Austria")
input("4 - Portugal")
user1 = input("Enter your answer:")

if user1 == first_answer:
score = score + 1
else:
score = score + 0

second_answer = 4

input("Q2 - Which is the fastest road car?")
input("1 - Ferrari")
input("2 - Lamborghini")
input("3 - McLaren")
input("4 - Bugatti")
user2 = input("Enter your answer:")

if user2 == second_answer:
score = score + 1
else:
score = score + 0

third_answer = 1

input("Q3 - Where doe Kia Motors come from?")
input("1 - South Korea")
input("2 - China")
input("3 - America")
input("4 - Japan")
user3 = input("Enter your answer:")

if user3 == third_answer:
score = score + 1
else:
score = score + 0

fourth_answer = 1

input("Q4 - Which of these is a British car company?")
input("1 - Jaguar")
input("2 - Ford")
input("3 - Ferrari")
input("4 - Hyundai")
user4 = input("Enter your answer:")

if user4 == fourth_answer:
score = score + 1
else:
score = score + 0

fifth_answer = 3

input("Q5 - Where was Ferrari created?")
input("1 - America")
input("2 - Canada")
input("3 - Italy")
input("4 - Spain")
user5 = input("Enter your answer:")

if user5 == fifth_answer:
score = score + 1
else:
score = score + 0

print(name, "has a score of", score)

Have been helping granddaughter to get started with Python and she has written a little program (below) which looks good but must have a flaw somewhere as final answer is always zero. Can anyone see where the error is? Thanks.

name = input("Hello, what is your name?")
print(name, ",welcome to the car quiz!")

first_answer = 2
score = 0
input("Q1 - Which country does Porsche come from?")
input("1 - England")
input("2 - Germany")
input("3 - Austria")
input("4 - Portugal")
user1 = input("Enter your answer:")

if user1 == first_answer:
score = score + 1
else:
score = score + 0

second_answer = 4

input("Q2 - Which is the fastest road car?")
input("1 - Ferrari")
input("2 - Lamborghini")
input("3 - McLaren")
input("4 - Bugatti")
user2 = input("Enter your answer:")

if user2 == second_answer:
score = score + 1
else:
score = score + 0

third_answer = 1

input("Q3 - Where doe Kia Motors come from?")
input("1 - South Korea")
input("2 - China")
input("3 - America")
input("4 - Japan")
user3 = input("Enter your answer:")

if user3 == third_answer:
score = score + 1
else:
score = score + 0

fourth_answer = 1

input("Q4 - Which of these is a British car company?")
input("1 - Jaguar")
input("2 - Ford")
input("3 - Ferrari")
input("4 - Hyundai")
user4 = input("Enter your answer:")

if user4 == fourth_answer:
score = score + 1
else:
score = score + 0

fifth_answer = 3

input("Q5 - Where was Ferrari created?")
input("1 - America")
input("2 - Canada")
input("3 - Italy")
input("4 - Spain")
user5 = input("Enter your answer:")

if user5 == fifth_answer:
score = score + 1
else:
score = score + 0

print(name, "has a score of", score)
Lets make it easy, since the code is pretty repetitive, and look at the first option only.

name = input("Hello, what is your name?")
print(name, ",welcome to the car quiz!")
 
first_answer = 2
score = 0
input("Q1 - Which country does Porsche come from?")
input("1 - England")
input("2 - Germany")
input("3 - Austria")
input("4 - Portugal")
user1 = input("Enter your answer:")
 
if user1 == first_answer:
score = score + 1
else:
score = score + 0
The main problem is you are trying to compare 'strings' with 'numbers' and the result will always be false. Note that you have defined 'score' and 'first_answer' as integers, while requesting from user1 a 'string' value. Remember, on it's own, the input() function returns a string. We can modify this behavior by telling the function what we want returned.
Thus
user1 = input("Enter your answer:")
becomes
user1 = int(input("Enter your answer:"))
Now your if/else statements will be comparing numbers with numbers.

One other point, all your 'input()s' on lines 6 thru 10 and the other segments as well, need only be 'print', having them as inputs requires the user to hit enter after every line, which I'm sure is not your intent.
Many thanks for your help, it was her first attempt and your suggestions are spot-on. I was a very competent COBOL programmer back in the 60's and enjoying getting back into it at 77!

I meant to add I suspect I will be back on here as time progresses :-)
(Apr-04-2018, 04:51 PM)Mugwash Wrote: [ -> ]I meant to add I suspect I will be back on here as time progresses :-)

Always glad to see returning members. Be sure and check out our other forums as well, such as Tutorials and Completed Scripts.

Hope both you and your granddaughter enjoy Python :-D
Thank you :-)