Python 随机数猜测游戏抛出错误:>;或<;不能在str和int之间使用
我有以下随机数字猜测游戏的代码:Python 随机数猜测游戏抛出错误:>;或<;不能在str和int之间使用,python,python-3.x,user-input,Python,Python 3.x,User Input,我有以下随机数字猜测游戏的代码: import random number = random.randint(1,100) name = input('Hi, Whats your name?') print ("Well", name, "i am thinking of a number between 1 and 100, take a guess") guess1 = input() if guess1 == number: print ("Good job, you go
import random
number = random.randint(1,100)
name = input('Hi, Whats your name?')
print ("Well", name, "i am thinking of a number between 1 and 100, take a guess")
guess1 = input()
if guess1 == number:
print ("Good job, you got it!")
while guess1 != number:
if guess1 > number:
print ('your guess is too high')
if guess1 < number:
print ('your guess is too low')
import random
def guessGame():
while True:
while True:
try:
low, high = map(int,input("Enter a lower number and a higher numer for your game.").split())
break
except ValueError:
print("Enter valid numbers please.")
if low > high:
print("The lower number can't be greater then the higher number.")
elif low+10 >= high:
print("At least lower number must be 10 less then the higher number")
else:
break
find_me = random.randint(low,high)
print("You have 6 chances to find the number...")
chances = 6
flag = 0
while chances:
chances-=1
guess = int(input("Enter your guess : "))
if guess<high and guess>low:
if guess < find_me:
print("The number you have entered a number less then the predicted number.",end="//")
print("{0} chances left.".format(chances))
elif guess > find_me:
print("The number you have entered a number greater then the predicted number.",end="//")
print("{0} chances left.".format(chances))
else:
print("Congrats!! you have succesfully guessed the right answer.")
return
else:
print("You must not input number out of range")
print("{0} chances left.".format(chances))
print("The predicted number was {0}".format(find_me))
随机导入
number=random.randint(1100)
name=input('你好,你叫什么名字?')
打印(“嗯”,名字,“我在想一个介于1和100之间的数字,猜猜看”)
猜测1=输入()
如果猜测1==数字:
打印(“干得好,你做到了!”)
而猜测1!=编号:
如果猜测1>数字:
打印('您的猜测太高')
如果1<数字:
打印('您的猜测太低')
这会抛出一个错误,即
或您可以在此处使用while循环-
逻辑应该是:
answer_is_correct = False
while not answer_is_correct :
Keep receiving input until answer is correct
您可以在while循环中检查条件是否正确。它可以通过以下方式完成
import random
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 100)
print('Well, ' + myName + ', I am thinking of a number between 1 and 100.')
inputNumber = int(raw_input('Enter the number')
while inputNumber != number:
print "Sorry wrong number , try again"
inputNumber = int(raw_input('Enter the number')
print "Congrats!"
我希望这对你有用:
import random
myname = input('Hello, what is your name?')
print('Well',myname,'am thinking of a number between 1 and 100')
number = random.randint(1,100)
guess = 0
while guess < 4:
guess_number = int(input('Enter a number:'))
guess += 1
if guess_number < number:
print('Your guess is to low')
if guess_number > number:
print('Your guess is to high')
if guess_number == number:
print('Your guess is correct the number is',number)
break
if guess == 4:
break
print('The number i was thinking of is',number)
随机导入
myname=input('你好,你叫什么名字?')
打印('嗯,我的名字,'我想到了一个介于1和100之间的数字')
number=random.randint(1100)
猜测=0
而猜测<4:
guess_number=int(输入('输入一个数字:'))
猜测+=1
如果猜数<数字:
打印('您的猜测太低')
如果猜测数量>数量:
打印('您的猜测过高')
如果guess_number==数字:
打印('您的猜测是正确的,数字是',数字)
打破
如果猜测=4:
打破
打印('我想到的数字是',数字)
您的代码中有两个错误
您需要将guess1的输入从字符串(默认情况下)转换为整数,然后才能将其与数字(整数)进行比较
while循环永远不会停止,因为您不允许用户输入另一个值
试试这个:
import random
number = random.randint(1,100)
name = input('Hi, Whats your name?')
print ("Well", name, "i am thinking of a number between 1 and 100, take a guess")
guess1 = int(input()) # convert input from string to integer
while guess1 != number:
if guess1 > number:
print ('your guess is too high. Try again.')
elif guess1 < number:
print ('your guess is too low. Try again.')
guess1 = int(input()) # asks user to take another guess
print("Good job, you got it!")
随机导入
number=random.randint(1100)
name=input('你好,你叫什么名字?')
打印(“嗯”,名字,“我在想一个介于1和100之间的数字,猜猜看”)
猜1=int(input())#将输入从字符串转换为整数
而猜测1!=编号:
如果猜测1>数字:
打印('您的猜测太高,请再试一次')
elif猜测1<数字:
打印('您的猜测太低,请再试一次')
guess1=int(input())#要求用户进行另一次猜测
打印(“干得好,你做到了!”)
来自随机导入*
def play_game():
打印(“让我们玩一个数字猜测游戏”)
#选择1到100之间的随机数
number=randint(1100)
choice=int(输入(“我想的是一个介于1和100之间的数字,你能猜出它是什么吗?”)
#引导用户进行正确的猜测
#循环将继续,直到用户猜到正确的数字
而选择!=编号:
如果选择<编号:
choice=int(输入(“太低了。您能再试一次吗?”)
elif选项>编号:
choice=int(输入(“太高了,可以再试一次吗?”)
continue\u game=input(“你猜对了!你想再玩一次吗?(是/否)”
#如果用户希望再次玩,请重新启动游戏
如果继续游戏==“Y”:
打印(“--”*42)
玩游戏
其他:
打印(“感谢播放:)”)
出口(0)
玩游戏
Python 3中用于猜游戏的代码:
import random
number = random.randint(1,100)
name = input('Hi, Whats your name?')
print ("Well", name, "i am thinking of a number between 1 and 100, take a guess")
guess1 = input()
if guess1 == number:
print ("Good job, you got it!")
while guess1 != number:
if guess1 > number:
print ('your guess is too high')
if guess1 < number:
print ('your guess is too low')
import random
def guessGame():
while True:
while True:
try:
low, high = map(int,input("Enter a lower number and a higher numer for your game.").split())
break
except ValueError:
print("Enter valid numbers please.")
if low > high:
print("The lower number can't be greater then the higher number.")
elif low+10 >= high:
print("At least lower number must be 10 less then the higher number")
else:
break
find_me = random.randint(low,high)
print("You have 6 chances to find the number...")
chances = 6
flag = 0
while chances:
chances-=1
guess = int(input("Enter your guess : "))
if guess<high and guess>low:
if guess < find_me:
print("The number you have entered a number less then the predicted number.",end="//")
print("{0} chances left.".format(chances))
elif guess > find_me:
print("The number you have entered a number greater then the predicted number.",end="//")
print("{0} chances left.".format(chances))
else:
print("Congrats!! you have succesfully guessed the right answer.")
return
else:
print("You must not input number out of range")
print("{0} chances left.".format(chances))
print("The predicted number was {0}".format(find_me))
随机导入
def guessGame():
尽管如此:
尽管如此:
尝试:
low,high=map(int,input(“为游戏输入一个较低的数字和一个较高的数字”)。split()
打破
除值错误外:
打印(“请输入有效数字”)
如果低>高:
打印(“较小的数字不能大于较大的数字。”)
elif低+10>=高:
打印(“至少较低的数字必须比较高的数字少10”)
其他:
打破
find_me=random.randint(低、高)
打印(“您有6次机会找到号码…”)
机会=6
标志=0
而机会:
机会-=1
guess=int(输入(“输入您的猜测:”)
如果猜测过低:
如果猜测<找到我:
打印(“您输入的数字小于预测数字。”,end=“/”)
打印(“{0}左。”。格式(机会))
elif guess>找到我:
打印(“您输入的数字大于预测数字。”,end=“/”)
打印(“{0}左。”。格式(机会))
其他:
打印(“恭喜!!你已经成功地猜到了正确的答案。”)
回来
其他:
打印(“您不能输入超出范围的数字”)
打印(“{0}左。”。格式(机会))
打印(“预测的数字为{0}”。格式(find_me))
您要求的是作业的核心部分。您尝试了什么,失败在哪里?作为一个注释,您最好在输入中包含输入文本:myName=input('Hello!您叫什么?')
以防止在print
语句后出现换行。投票反对这个问题,因为这是一个常见错误,在谷歌搜索时会出现“python数字猜测游戏”,允许关闭其他副本