python - 运行程序python时出错,
问题描述
import random
z = random.randint(1,10)
guess = input("Enter a guess 1-10: ")
guess = int(guess)
if (guess > z):
print("To high, try again!")
guesshighs = input("Last try, enter a number 1-10: ")
guesshighs = int(guesshighs)
if (guesshighs > z):
print("Damn it! You were to high again! The answer was, " + z )
elif (guesshighs < z):
print("Nice try but you were to small! The answer was, " + z)
else:
print("Nice you got it!")
elif (guess < z):
print("To small, try again!")
guesssmallh = input("Last try, enter a number 1-10: ")
guesssmallh = int(guesssmallh)
if (guesssmallh < z):
print("Almost but you were to small! The number was, " + z)
elif (guesssmallh > z):
print("Soooo close but you were to high! The number was, " + z)
else:
print("Nice one you did it!")
else:
print("Nice you guessed correct!")
这是一个随机数猜谜游戏。当您选择一个数字并且它说它太小或太大并且在第一次尝试后再次弄错数字时,它会给出错误而不是说不错尝试但答案是(ANSWER)。
解决方案
print("Damn it! You were to high again! The answer was, " + z )
Z 是一个 int 并且你将它与一个字符串相加,你可以使用“,”而不是“+”
像这样:
import random
z = random.randint(1,10)
guess = input("Enter a guess 1-10: ")
guess = int(guess)
if (guess > z):
print("To high, try again!")
guesshighs = input("Last try, enter a number 1-10: ")
guesshighs = int(guesshighs)
if (guesshighs > z):
print("Damn it! You were to high again! The answer was, " , z )
elif (guesshighs < z):
print("Nice try but you were to small! The answer was, " , z)
else:
print("Nice you got it!")
elif (guess < z):
print("To small, try again!")
guesssmallh = input("Last try, enter a number 1-10: ")
guesssmallh = int(guesssmallh)
if (guesssmallh < z):
print("Almost but you were to small! The number was, " , z)
elif (guesssmallh > z):
print("Soooo close but you were to high! The number was, " , z)
else:
print("Nice one you did it!")
else:
print("Nice you guessed correct!")
推荐阅读
- sql-server - 需要根据数据的值将数据透视到 MS SQL 中的对应列
- c# - 根据值获取渐变中的颜色值
- python - 如何检查页面是否已被访问?(我在用户模型上使用了一个数组字段,我正在尝试在其中添加页面 ID)
- reactjs - 在 React select 中使用枚举
- javascript - 如何在 Javascript 中创建准确的 setTimeout(不是间隔)?
- c# - Odata 不支持“GroupBy”方法
- c++ - 无效函数使用相同的变量打印两个不同的答案
- swift - 带有持续时间的循环动画,Swift 4
- javascript - 如何关闭手风琴菜单的打开部分?
- python - Scrapy/Python 请求优先级和 CONCURRENT_REQUESTS