循环以获得问题的答案 [英] Looping to get answers to a question

查看:61
本文介绍了循环以获得问题的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到循环问题。当我选择1时,它会回答一个问题,然后崩溃并说出有关用户输入的信息。我很困惑,因为它说的是关于无法解决的类型。我希望它在我按下5时退出



我尝试过:



I'm having problems with looping. When I pick 1 it answers one question then crashes and says something about userinput. I am pretty confused as it's saying something about unorderable types. I want it to exit when I press 5 as well

What I have tried:

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"))
	
		return userInput
		
		
def menu_choice():
	score = 0
	fig1 = random.randrange(0,21)
	fig2 = random.randrange(0,10)
	pick = input('Choose Menu choice')
	pick = int(pick)
	
	
	if pick is 1:
		qas = fig1+fig2
		print('What is ',str(fig1)+ '+',str(fig2)+'?\n')
		ans = int(input(''))
		if ans == qas:
			print('Correct')
			score = score +1
		else:
			print('Incorrect')
			return pick
			
		
			
	if pick is 2:
		qas = fig1-fig2
		print('what is ', str(fig1)+ '-',str(fig2)+'?\n')
		ans = int(input(''))
		if ans == qas:
			print('Correct')
		else:
			print('Incorrect')
			
	
					
	
 
def main():
	
    displayIntro()
    mainMenu()
    menu_choice()
    userInput()
    while option != 5:
        option = userInput()
    print("\n You have choosen to Exit.")
    
 
main()
#displayIntro()
#mainMenu()
#menu_choice()
#userInput()

推荐答案

首先,这是罪魁祸首错误消息是

First, the culprit to this error msg is
userInput = (input("Enter a choice "))
	while userInput >5 or userInput <=0:


Python中的
输入返回一个字符串数据类型,并且您正在尝试将字符串与int进行比较。不,不。首先尝试转换它:


input in Python returns a string data type, and you are trying to compare string against int. No no. Try to convert it first:

int(userInput) > 5 or int(userInput) <=0:



其次,您将在此处获得选项变量的未定义错误:


Second, you are going to get an undefined error for the option variable in here:

userInput()
while option != 5:
    option = userInput()



我认为您的选项是从userInput()获取值。所以解决它。

第三,这个while循环不能按你的意愿工作。为什么?这和第一个错误一样。您可以将选项转换为int或将5转换为字符串。


I think your option is meant to take the value from userInput(). So fix it.
Third, this while loop above will not work as you want it. Why? It is the same mistake as the first. Either you convert the option to int or make the 5 into a string.


这是我修复了它运行的项目的这一部分的缩进,但是当我选择一个数字时,它给了我错误 [ ^ ],我已经给你一些建议。
This is a repost of I fixed the indentation on this part of my project it runs but when I choose a number it gives me errors[^], and I have already given you a nmuber of suggestions.


这篇关于循环以获得问题的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆