首页 > 解决方案 > 运行程序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)。

标签: python

解决方案


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!")

推荐阅读