Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if statement not working
#1
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
Reply
#2
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()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
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
#4
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.
Reply
#5
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?
Reply
#6
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.")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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:
Reply
#8
(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
Reply
#9
Did you look at my example in the previous post?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question If, elif, and else statement not working PickleScripts 3 839 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  If statement not working correctly? MrKnd94 2 796 Nov-16-2022, 02:49 AM
Last Post: deanhystad
  If Statement not working...Why? Milfredo 2 2,161 Oct-17-2020, 03:23 AM
Last Post: Milfredo
  Invoking function in if else statement, not working! ibaad1406 13 5,465 May-30-2019, 09:05 PM
Last Post: ibaad1406
  If statement not working oldcity 4 3,048 Oct-14-2018, 10:45 AM
Last Post: oldcity
  why is this try.....except statement not working? HenryJ 3 8,708 Feb-06-2018, 06:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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