gpt4 book ai didi

python - 如何通过并取消这个小程序

转载 作者:太空宇宙 更新时间:2023-11-03 21:42:44 25 4
gpt4 key购买 nike

我的程序问题是它会告诉我我的变量“guess”是我会的这是说“猜得更低”的 n。

Traceback (most recent call last):
F
guess = int(input("Guess a number: "))
ValueError: invalid literal for int() with base 10: 'hfdg'

这是该程序的代码

import random

random_number = random.randint(1, 10)
tries = 0


print ("Enter yes, or no")

saysalse

while not says_yes or not says_no:
player_input = input("Would you like to play a game?: ")
player_input = player_input.lower()
if player_input == "yes":
says_yes = True
break
elif player_input == "no":
says_no = True
print("See you next time.")
exit()

if says_yes:
print("Ok, great.")
print("How this game works is that you are going to guess a number ranging from 1-10 \
and if you guess it right then you win")
guess = int(input("Guess a number: "))
choose a number between 1-10.")
guess = int(input("Guess a number: "))

while int(guess) != int(random_number):
tries to guess the number.")

最佳答案

看这里:

if says_yes:
print("Ok, great.")
print("How this game works is that you are going to guess a number ranging from 1-10 \
and if you guess it right then you win")
guess = int(input("Guess a number: "))

while guess > 10 or guess < 1:
print("Please choose a number between 1-10.")
guess = int(input("Guess a number: "))

Pycharm 说这个错误,因为可能会发生“says_yes”为 False 并且输入将出现的情况,然后猜测未定义,我知道你有一个 exit() 但 pycharm 很挑剔。

这是您的完整代码:

import random

random_number = random.randint(1, 10)
tries = 0

print("Enter yes, or no")

says_yes = False
says_no = False

while not says_yes or not says_no:
player_input = input("Would you like to play a game?: ")
player_input = player_input.lower().strip()
if player_input == "yes":
says_yes = True
break
elif player_input == "no":
says_no = True
print("See you next time.")
exit()
else:
print("You have to think about it again!")

if says_yes:
print("Ok, great.")
print("How this game works is that you are going to guess a number ranging from 1-10 and if you guess"
" it right then you win")
while True:
raw_guess = input("Guess a number: ")
try:
guess = int(raw_guess)
except ValueError:
print("Try it again, this was not a number!")
else:
if guess > 10 or guess < 1:
print("Please choose a number between 1-10.")
elif guess > random_number:
print("Guess lower")
tries += 1
elif guess < random_number:
print("Guess higher")
tries += 1
else:
break

print("It took you " + str(tries) + " tries to guess the number.")

关于python - 如何通过并取消这个小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52730982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com