我修改了它运行的项目的这一部分的缩进,但是当我选择一个数字时,它给了我错误 [英] I fixed the indentation on this part of my project it runs but when I choose a number it gives me errors
本文介绍了我修改了它运行的项目的这一部分的缩进,但是当我选择一个数字时,它给了我错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import random
# 这是我的菜单选项,用户可以从+, - ,*,/中选择,否则用户会选择5退出。
def mainMenu():
menu_pick = [ 1。+ , 2. - , 3。*, 4。 /, 5.退出]
print (menu_pick [ 0 ])
print ( menu_pick [ 1 ])
print (menu_pick [ 2 ])
打印(menu_pick [ 3 ])
print (menu_pick [ 4 ])
# 此功能显示首次开始测验时用户将获得的介绍。
def displayIntro():
# 打印介绍和名称的输出
print ( 欢迎使用随机数学测验!什么是你的名字)
# 输入的名称将被存储在输入中
名称=输入()
# 然后欢迎用户加上名称存储在输入中并告诉用户开始游戏。
print ( 欢迎, +名称+ \ n让我们开始)
def userInput():
userInput =(input( 输入选项))
而 userInput > 5 或 userInput< = 0:
print ( 菜单选项不正确)
userInput =(输入( 请再试一次))
else :
return userInput
def menu_choice():
while counter< 10:
fig1 = random.randint( 0 , 12 )
fig2 = random.randint( 0 , 6 )
function = random.choice(function)
question = print (图1,函数,图2,' =' )
输入(' 答案:')
counter = counter + 1
if function == ' 跨度> 1':
count == fig1 + fig2
if count == int(回答):
print (' 正确!')
得分=得分+ 1
其他:
print (' 错误')
elif function == ' 2':
count == fig1 - fig2
if count == int(answer) :
print (' 正确')
得分=得分+ 1
其他:
< span class = code-keyword> print (' 错误')
elif function == ' 3' :
count == fig1 * fig2
if count == int(answer):
print (' 更正')
得分=得分+ 1
else :
print (' 错误')
elif function == ' 4':
count == fig1 / fig2
if count == int(answer):
print (< span class =c ode-string>' 正确')
得分=得分+ 1
else :
print (' 错误')
def main():
displayIntro()
mainMenu()
menu_choice()
option = userInput()
while 选项!= 5 :
option = userInput()
< span class =code-keyword> print ( \ n您已选择退出。 )
main()
我尝试了什么:
i修复了菜单选项上的缩进我想让它与我选择的1-4 t菜单选项一起使用o使用函数,但一直给我错误任何帮助请使用python 3
解决方案
你确定else
在这个地方?
def userInput():
userInput =(输入( 输入选项))
while userInput> 5 或 userInput< = 0:
print (< span class =code-string> 菜单选项不正确)
userInput =(输入( 请再试一次))
else :
return userInput
[UpDate]
引用:是的因为如果我选择5退出它,我需要它l贯穿循环
你真的确定吗?
对我来说,你需要总是返回userInput
。进入循环并不重要。
def userInput():
userInput =(input( 输入选项))
userInput> ; 5 或 userInput< = 0:
print ( 不正确的菜单选项)
userInput =(输入( 请再试一次))
return userInput
第32行:
而计数器< 10:
您尚未在该模块中声明counter
。
import random
# 这是我的菜单选项,用户可以从+, - ,*,/中选择,否则用户会选择5退出。
def mainMenu():
menu_pick = [ 1。 +, 2。 - , 3。 *, 4。 /, 5。退出]
print (menu_pick [ 0 ])
print (menu_pick [ 1 ])
print (menu_pick [ 2 ])
print (menu_pick [ 3 ])
print (menu_pick [ 4 ])
# 此函数显示首次启动时用户将获得的介绍测验。
def displayIntro():
# 打印介绍和名称的输出
print ( 欢迎来到随机数学测验!你的名字是什么)
# 输入的名称将存储在输入中
Name = input()
# 然后欢迎用户加上存储在输入中的名称并告诉用户开始游戏。
print ( Welcome , +名称+ \ n让我们开始)
def userInput():
userInput =(输入( 输入选项))
,而 userInput> 5 或 userInput< = 0:
print ( 不正确的菜单选项)
userInput =(inp ut( 请再试一次))
else :
return userInput
def menu_choice():
function =( +, - , *, /)
counter = 0
而计数器< 10:
fig1 = random.randint( 0 , 12 )
fig2 = random.randint( 0 , 6 )
function = random.choice(function)
question = print (图1,函数, fig2,' =')
输入(' 答案:')
counter = counter + 1
if function == ' 1':
count == fig1 + fig2
if count == int(answer):
print (' 正确!')
得分=得分+ 1
else :
print (' 不正确')
elif function == ' 2':
count == fig1 - fig2
如果温泉n> count == int(answer):
print (' 正确')
得分=得分+ 1
else :
print (' 不正确')
elif function == ' 3':
count == fig1 * fig2
if count == int(answer) :
print (' 正确')
得分=得分+ 1
其他:
< span class =code-keyword> print (' 错误')
elif function == ' 4':
count == fig1 / fig2
if count == int(answer):
print (' 正确')
得分=得分+ 1
else :
print (' 错误')
def main():
displayIntro()
menu_choice()
mainMenu()
option = userInput()
while 选项!= 5 :
option = userInput()
print ( \ n您已选择退出。)
main()
import random
# this is my menu choice that the user can select from +,-,*,/ or else user would select 5 to exit.
def mainMenu():
menu_pick = ["1. + ", "2. - ", "3. * ", "4. / ", "5. Exit"]
print(menu_pick[0])
print(menu_pick[1])
print(menu_pick[2])
print(menu_pick[3])
print(menu_pick[4])
#this function displays the introduction that the user would be given when first start the quiz.
def displayIntro():
#prints the output of introduction and name
print("Welcome to Random Maths Quiz ! What is your Name ")
#what ever name was entered it will be stored in input
Name = input()
#then will welcome the user plus there name that was stored in input and tell the user to begin the game.
print("Welcome , "+ Name + "\n Lets Begin")
def userInput():
userInput = (input("Enter a choice "))
while userInput >5 or userInput <=0:
print("Not correct menu choice")
userInput = (input("Please Try Again"))
else:
return userInput
def menu_choice():
while counter <10:
fig1 = random.randint(0,12)
fig2 = random.randint(0,6)
function = random.choice(function)
question = print(fig1,function ,fig2, '=')
input('Answer:')
counter = counter +1
if function == '1':
count == fig1 + fig2
if count == int(answer):
print('correct!')
score = score+1
else:
print ('Incorrect')
elif function == '2':
count == fig1 - fig2
if count == int (answer):
print('Correct')
score = score+1
else:
print ('Incorrect')
elif function == '3':
count == fig1 * fig2
if count == int (answer):
print('Correct')
score =score+1
else:
print('Incorrect')
elif function == '4':
count == fig1 / fig2
if count == int(answer):
print('Correct')
score = score+1
else:
print('Incorrect')
def main():
displayIntro()
mainMenu()
menu_choice()
option = userInput()
while option != 5:
option = userInput()
print("\n You have choosen to Exit.")
main()
What I have tried:
i have fixed the indentation on the menu choice i want to get it to work with whatever menu option i choose from 1-4 to work with functions but keeps giveing me errors any help please im using python 3
解决方案
Are you sure about theelse
at this place ?
def userInput(): userInput = (input("Enter a choice ")) while userInput >5 or userInput <=0: print("Not correct menu choice") userInput = (input("Please Try Again")) else: return userInput
[UpDate]
Quote:yes cuz i need it for if i choose 5 to exit it will run through the loop
Are you really sure ?
For me you need to always return theuserInput
. Having enterd the loop does not matter.
def userInput(): userInput = (input("Enter a choice ")) while userInput >5 or userInput <=0: print("Not correct menu choice") userInput = (input("Please Try Again")) return userInput
Line 32:
while counter <10:
You have not declaredcounter
in that module.
import random # this is my menu choice that the user can select from +,-,*,/ or else user would select 5 to exit. def mainMenu(): menu_pick = ["1. + ", "2. - ", "3. * ", "4. / ", "5. Exit"] print(menu_pick[0]) print(menu_pick[1]) print(menu_pick[2]) print(menu_pick[3]) print(menu_pick[4]) #this function displays the introduction that the user would be given when first start the quiz. def displayIntro(): #prints the output of introduction and name print("Welcome to Random Maths Quiz ! What is your Name ") #what ever name was entered it will be stored in input Name = input() #then will welcome the user plus there name that was stored in input and tell the user to begin the game. print("Welcome , "+ Name + "\n Lets Begin") def userInput(): userInput = (input("Enter a choice ")) while userInput >5 or userInput <=0: print("Not correct menu choice") userInput = (input("Please Try Again")) else: return userInput def menu_choice(): function = ("+","-","*","/") counter =0 while counter <10: fig1 = random.randint(0,12) fig2 = random.randint(0,6) function = random.choice(function) question = print(fig1,function ,fig2, '=') input('Answer:') counter = counter +1 if function == '1': count == fig1 + fig2 if count == int(answer): print('correct!') score = score+1 else: print ('Incorrect') elif function == '2': count == fig1 - fig2 if count == int (answer): print('Correct') score = score+1 else: print ('Incorrect') elif function == '3': count == fig1 * fig2 if count == int (answer): print('Correct') score =score+1 else: print('Incorrect') elif function == '4': count == fig1 / fig2 if count == int(answer): print('Correct') score = score+1 else: print('Incorrect') def main(): displayIntro() menu_choice() mainMenu() option = userInput() while option != 5: option = userInput() print("\n You have choosen to Exit.") main()
这篇关于我修改了它运行的项目的这一部分的缩进,但是当我选择一个数字时,它给了我错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文