Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Having problems with if statements in python

User8574

Active Coder
This quiz project doesn't work.

Python:
print("History Quiz")
total_score = 0

print()

question1 = input("For how many years did WW2 last? - 3, 5, 6 or 8? ")
if int(question1) == 6:
    print("Correct!")
else:
    print("Better luck next time!")

print()

question2 = input("In what year did Harry Truman take power in the United States? - 1944, 1945, 1946, or 1948? ")
if int(question2) == 1945:
    print("Amazing!")
else:
    print("Incorrect! :/")

print()

question3 = input("In what year did the Soviet Union collapse? - 1990, 1993, 1989, or 1991 ")
if int(question3) == 1991:
    print("Absoloutely! :)")
else:
    print("Try again next time!")

print()

question4 = input("What did American prohibition restrict? - a = Alchohol, b = Drugs, c = Milk, or d = Meat ")
if question4.upper() == "A":
    print("Of course!")
else:
    print("I expected better!")

print()

question5 = input("in what year did Uganda gain independence? 1960, 1961, 2010, or 1936 ")
if int(question5) == 1961:
    print("Well Done!")
else:
    print("No!")

print()

if int(question1) == 6:
    total_score = total_score + 1

if int(question2) == 1945:
    total_score = total_score + 1

if int(question3) == 1991:
    total_score = total_score + 1

if question4.upper() == "A":
    total_score = total_score + 1

if int(question5) == 1961:
    total_score = total_score + 1

print("Your total score is:", total_score,"out of 5")

if total_score == >3 and >5:
    print("Good score!")

elif total_score == <4 and >1:
    print("Try harder next time!")

elif total_score == <2:
    print("You don't know anything. Read more books!")

else:
    print("You are a genius! Read less books!")

For some reason, line 63 (if total_score == >3 and >5) is shown as an error. Anyone know why? I think it is because of the less than and more than signs but I'm not sure how to fix it
 
Solution
Heres the fixed code @getrickrolled. To compare values in Python, you have to use `>=` (greater than), and `<=` (less than). You also made another mistake new Python coders make. Conditions seperated by `and` / `or` are completely seperate, so instead of doing `total_score == >3 and >5` you should do `total_score >= 3 and total_score > 5`.

Python:
print("History Quiz")

total_score = 0


print()


question1 = input("For how many years did WW2 last? - 3, 5, 6 or 8? ")

if int(question1) == 6:

    print("Correct!")

else:

    print("Better luck next time!")


print()


question2 = input("In what year did Harry Truman take power in the United States? - 1944, 1945, 1946, or 1948? ")

if int(question2) == 1945:

    print("Amazing!")...
I'm not a Python buff but I guess line 63 should be

Python:
if total_score >3 and total_score >5:

At least that's how it would be in other languages, I don't think Python is that different.
This would probably make it run although it doesn't make sense logically. The score can't be bigger than 5, and even if it could, it would surely be bigger than three 😉

And same kind of change for line 66.
 
Heres the fixed code @getrickrolled. To compare values in Python, you have to use `>=` (greater than), and `<=` (less than). You also made another mistake new Python coders make. Conditions seperated by `and` / `or` are completely seperate, so instead of doing `total_score == >3 and >5` you should do `total_score >= 3 and total_score > 5`.

Python:
print("History Quiz")

total_score = 0


print()


question1 = input("For how many years did WW2 last? - 3, 5, 6 or 8? ")

if int(question1) == 6:

    print("Correct!")

else:

    print("Better luck next time!")


print()


question2 = input("In what year did Harry Truman take power in the United States? - 1944, 1945, 1946, or 1948? ")

if int(question2) == 1945:

    print("Amazing!")

else:

    print("Incorrect! :/")


print()


question3 = input("In what year did the Soviet Union collapse? - 1990, 1993, 1989, or 1991 ")

if int(question3) == 1991:

    print("Absoloutely! :)")

else:

    print("Try again next time!")


print()


question4 = input("What did American prohibition restrict? - a = Alchohol, b = Drugs, c = Milk, or d = Meat ")

if question4.upper() == "A":

    print("Of course!")

else:

    print("I expected better!")


print()


question5 = input("in what year did Uganda gain independence? 1960, 1961, 2010, or 1936 ")

if int(question5) == 1961:

    print("Well Done!")

else:

    print("No!")


print()


if int(question1) == 6:

    total_score = total_score + 1


if int(question2) == 1945:

    total_score = total_score + 1


if int(question3) == 1991:

    total_score = total_score + 1


if question4.upper() == "A":

    total_score = total_score + 1


if int(question5) == 1961:

    total_score = total_score + 1


print("Your total score is:", total_score,"out of 5")


if total_score >= 3 and total_score > 5:

    print("Good score!")


elif total_score <= 4 and total_score > 1:

    print("Try harder next time!")


elif total_score <= 2:

    print("You don't know anything. Read more books!")


else:

    print("You are a genius! Read less books!")
 
Solution
if total_score >= 3 and total_score > 5:
I stubbornly object to this line 🙂 This is totally the same as if total_score > 5:
Undoubtedly this should have been

if total_score >= 3 and total_score < 5:

Quite a pity that Python does not have a switch--case statement like all modern languages (bash, perl, C, C++, C#, Javascript, Java, ...)
That would have been so much cleaner. Why did they decide to be so different ?
 
Last edited by a moderator:
I stubbornly object to this line 🙂 This is totally the same as if total_score > 5:
Undoubtedly this should have been

if total_score >= 3 and total_score < 5:

Quite a pity that Python does not have a switch--case statement like all modern languages (bash, perl, C, C++, C#, Javascript, Java, ...)
That would have been so much cleaner. Why did they decide to be so different ?
newer versions of python has a switch case

example

Python:
response_code = 404

match response_code:
    case 400:
        print("Server can't understand.")
    case 401 | 403:
        print("Server refused to send back.")
    case 404:
        print("Server can't find.")

# Returns:
# Server can't find.
 
newer versions of python has a switch case

example

Python:
response_code = 404

match response_code:
    case 400:
        print("Server can't understand.")
    case 401 | 403:
        print("Server refused to send back.")
    case 404:
        print("Server can't find.")

# Returns:
# Server can't find.
Ah ok. Still they just had to be different didn't they, calling it match rather than switch. But hey, what's in a name
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom