首页 > 解决方案 > 当你猜对时,数字猜测器打印错误

问题描述

所以我正在做一个数字猜测器,当我做对时,它会打印出“错误”的声明,有人可以帮我吗?

#randomly select number

import random

#Selects number

numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50

#tell the user to pick a number

guess = input("I am think of a number between 1 and 50, Guess what number ")

num = random.choice(numbers) #selecting the number

#prints if you got it right

if num == guess:
    print("Yes you go it! You rock.")

else:
    print("WRONG! I was thinking of", num, "Try again! Better luck next time.")

标签: python

解决方案


你应该做两件事

  • 转换guess为整数guess = int(input("..."))if num == int(guess). (这将解决您遇到的问题)。
  • 与其制作数字列表并随机选择,不如使用random.randint(). num = random.randint(1,50). 然后您可以删除该numbers

推荐阅读