Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding error?
#1
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)
Reply
#2
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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 :-)
Reply
#4
(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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
Thank you :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to coding. An error occurs with no definition westernwhiskey 4 2,996 Mar-15-2018, 12:37 PM
Last Post: westernwhiskey
  Python Coding Error MGallo 3 3,782 Jul-20-2017, 02:35 PM
Last Post: MGallo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020