所以,在编程方面,我是一个完整的新手。我一直在看教程,我正在读一本关于如何编程python的书。所以,我想自己创建一个数字生成器猜测器,我已经看过一些教程,但我不想重新创建代码。基本上,我想用我得到的信息做出我自己的猜测。
import random
# Random Numbergenerator Guesser
print("Hello and welcome to the random number guesser.")
print("I am guessing a number of 1 - 20. Can you guess which one?")
x = random.randint(1,20)
# Here you guess the number value of 'x'
for randomNumber in range (1,7):
randomGuess = input()
if randomGuess > x:
print("Too high. Guess again!")
elif randomGuess < x:
print("Too low. Guess again!")
else:
break
# Checks to see if the number you were guessing is correct or takes you to a fail screen.
if randomGuess == x:
print("Correct number!")
else:
print("Too many tries. You have failed. The number I was thinking of was " + (x))``
我一直收到这个错误。
C:\Python\Python35\python.exe "C:/Users/Morde/Desktop/Python Projects/LoginDataBase/LoginUserDatabse1File.py"
Hello and welcome to the random number guesser.
I am guessing a number of 1 - 20. Can you guess which one?
1
Traceback (most recent call last):
File "C:/Users/Morde/Desktop/Python Projects/LoginDataBase/LoginUserDatabse1File.py", line 12, in <module>
if randomGuess > x:
TypeError: unorderable types: str() > int()
答案 0 :(得分:0)
首先,你的格式化很糟糕。其次,提出错误是因为您正在比较string
(来自input()
)和integer
。您应该使用以下内容将输入值转换为integer
randomGuess = int(input())
答案 1 :(得分:0)
错误出现在这一行:
if randomGuess > x: print("Too high. Guess again!")
因为您尝试将整数x
与字符串randomGuess
进行比较
而randomGuess
是一个字符串,因为你这样定义它:
randomGuess = input()
你可以这样做:
randomGuess = int(input())
强制python将您的猜测数视为整数(但是当用户输入除整数之外的其他内容时,您必须处理这种情况)