Python Forum
if statement not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: if statement not working (/thread-8819.html)



if statement not working - trent101010 - Mar-08-2018

Hi,
I'm trying to make a python game called Math God, which has three subjects:
1. Quadratics
2. Interest
3. Order of Ops.

I'm trying to make an if statement inside the try statement, but I keep getting a bad input error.

Here's my code.


#Math God
#By Trenton
#Made in 2018
#You answer questions correctly in under a minute. Each correct answer is worth 50 points. Get one wrong and you get a strike. Three strikes and the game ends. There are three subjects: quadratics, interest, and order of ops. Each contain 50 questions selected at random within that subject.
input("Math God				 Press any key to start")
print("Welcome to Math God! Made by Trent in 2018!")
print("In this game, you have a minute to answer the math problem correctly. If you get it wrong or run out of time, you get a strike. Three strikes and the game is over.")
try:
  x=input("There are three modes to this game: quadratic, interest, and order of operations. Type 'q' for quadratics, 'i' for interest, and 'o' for order of ops.")
if x = 'p':
  input("You have selected quadratics. Press any key to start."
else 
    if x = 'i':
        input("You have selected interest. Press any key to start.")
    else 
        if x = 'o':
          input("You have selected order of operations. Press any key to start.")
  except:
  input("Sorry, but you didn't correctly choose a valid mode. Press any key to continue.")
return
If you could tell me what I'm doing wrong, that would be great


RE: if statement not working - wavic - Mar-08-2018

Fix the indentation to equal spaces. The if block inside the try/except must be indented.
You can't use just input() in a script without an assignment.
x = input()
not just
input()



RE: if statement not working - sparkz_alot - Mar-08-2018

Without really getting into the code (there are a number of errors), I would say you need to indent your first if/else statement. In comparisons, you need to use '==' not '='. Change your first 'else' to an 'elif == i' and so on.
To get you started, basically you want

print("Math God")
print("Welcome to Math God! Made by Trent in 2018!")
print("In this game, you have a minute to answer the math problem correctly. If you get it wrong or run out of time, you get a strike. Three strikes and the game is over.")
while True:
    x = input("There are three modes to this game: quadratic, interest, and order of operations. Type 'q' for quadratics, 'i' for interest, and 'o' for order of ops.")
    try:
        if x == 'q':
           print("You have selected quadratics. Press any key to start.")
        elif x == 'i':
           print("You have selected interest. Press any key to start.")
        elif x == 'o':
           print("You have selected order of operations. Press any key to start.")
    except:
        print("Sorry, but you didn't correctly choose a valid mode. Press any key to continue.")
        # return
This is just one way to go, there are others, but hopefully you get the idea.


RE: if statement not working - trent101010 - Mar-08-2018

Quote:Without really getting into the code (there are a number of errors), I would say you need to indent your first if/else statement. In comparisons, you need to use '==' not '='. Change your first 'else' to an 'elif == i' and so on.
To get you started, basically you want
print("Math God")
print("Welcome to Math God! Made by Trent in 2018!")
print("In this game, you have a minute to answer the math problem correctly. If you get it wrong or run out of time, you get a strike. Three strikes and the game is over.")
while True:
    x = input("There are three modes to this game: quadratic, interest, and order of operations. Type 'q' for quadratics, 'i' for interest, and 'o' for order of ops.")
    try:
        if x == 'q':
           print("You have selected quadratics. Press any key to start.")
        elif x == 'i':
           print("You have selected interest. Press any key to start.")
        elif x == 'o':
           print("You have selected order of operations. Press any key to start.")
    except:
        print("Sorry, but you didn't correctly choose a valid mode. Press any key to continue.")
        # return
I got a bad input error on line 8

Quote:Fix the indentation to equal spaces. The if block inside the try/except must be indented.
You can't use just input() in a script without an assignment.
I was just trying for the other inputs to make a "Press any key to continue." kind of thing.


RE: if statement not working - trent101010 - Mar-11-2018

I tried this:
#Math God
#By Trenton Varnadoe
#Made in 2018
#You answer questions correctly in under a minute. Each correct answer is worth 50 points. Get one wrong and you get a strike. Three strikes and the game ends. There are three subjects: quadratics, interest, and order of ops. Each contain 50 questions selected at random within that subject.
import time
print("Welcome to Math God! Made by Trent Varnadoe in 2018!")
print("In this game, you have a minute to answer the math problem correctly. If you get it wrong or run out of time, you get a strike. Three strikes and the game is over.")
while True:
	mode = str(input("There are three modes to this game: quadratic, interest, and order of operations. Type 'q' for quadratics, 'i' for interest, and 'o' for order of ops."))
	try:
		if mode = 'p':
  			print("You have selected quadratics.")
		elif mode == 'i':
  			print("You have selected interest.")
		elif mode == 'o':
  			print("You have selected order of operations.")
  	except:
  		input("Sorry, but you didn't correctly choose a valid mode. Press any key to continue.")
and this is what I got:
PauseError: Bad input on line 11.
Any idea on what I'm doing wrong?


RE: if statement not working - wavic - Mar-11-2018

In line 11 change = with ==
You and fix the while loop cause you have an infinite loop. There is no need of try/except here.
Any input is a string so no need for conversion.

The choices don't correspond to the input. You are asking for 'q' but there is 'p'.

while True:
    mode = input("There are three modes to this game: quadratic, interest, and order of operations. Type 'q' for quadratics, 'i' for interest, and 'o' for order of ops.")

    if mode == 'p':
        print("You have selected quadratics.")
        break
        
    elif mode == 'i':
        print("You have selected interest.")
        break
        
    elif mode == 'o':
        print("You have selected order of operations.")
        break
        
    else:
        print("Sorry, but you didn't correctly choose a valid mode. Press any key to continue.")



RE: if statement not working - Larz60+ - Mar-11-2018

To make the initial input look somewhat better, i'd change it to:
x = input("There are three modes to this game: quadratic, interest, and order of operations. Type:"
          "\n    'q' for quadratics\n    'i' for interest\n    'o' for order of ops\nYour choice: ")
so it looks like:
Output:
There are three modes to this game: quadratic, interest, and order of operations. Type: 'q' for quadratics 'i' for interest 'o' for order of ops Your choice:



RE: if statement not working - trent101010 - Mar-12-2018

(Mar-11-2018, 07:07 PM)wavic Wrote: In line 11 change = with ==
You and fix the while loop cause you have an infinite loop. There is no need of try/except here.
Any input is a string so no need for conversion.

The choices don't correspond to the input. You are asking for 'q' but there is 'p'.

while True:
    mode = input("There are three modes to this game: quadratic, interest, and order of operations. Type 'q' for quadratics, 'i' for interest, and 'o' for order of ops.")

    if mode == 'p':
        print("You have selected quadratics.")
        break
        
    elif mode == 'i':
        print("You have selected interest.")
        break
        
    elif mode == 'o':
        print("You have selected order of operations.")
        break
        
    else:
        print("Sorry, but you didn't correctly choose a valid mode. Press any key to continue.")

You're right, I have an infinite loop. What do I do?
Sorry, I am just starting Python. I have basically no idea what I'm doing. Confused


RE: if statement not working - wavic - Mar-14-2018

Did you look at my example in the previous post?