python - AttributeError: 'NoneType' 对象没有属性写猜谜游戏
问题描述
更新的代码,让它工作,但我在我的 while 循环上方和下方重复行。任何友好的建议来解决这个错误,只是寻找一些指导。
import pyperclip
import random
x=random.randint(1,10)
pyperclip.copy(x)
print(str(int(pyperclip.paste())))
def guessingGame(answer):
if(casting==x):
print("correct")
return True
elif(casting<x):
print("higher")
return False
else:
print("lower")
return False
userAnswer=input()
casting=int(userAnswer)
guessingGame(casting)
while(guessingGame!=True):
userAnswer=input()
casting=int(userAnswer)
guessingGame(casting)
我正在尝试创建一个简单的猜谜游戏,并且遇到了AttributeError
. 这是导致错误的输入:
lets play a guessing game, can you get our number? type in your guess
2
Traceback (most recent call last):
File "E:/guessing.py", line 25, in <module>
while(guess(userAnswer)==False):
File "E:/guessing.py", line 9, in guess
print(str(int(copying.paste()))+" higher")
AttributeError: 'NoneType' object has no attribute 'paste'
这是我的代码:
import pyperclip
import random
x=random.randint(1,10)
def guess(value):
copying=pyperclip.copy(int(value))
if(value<x):
print(str(int(copying.paste()))+" higher")
return False
elif(value>x):
copying=pyperclip.copy(value)
print(str(int(copying.paste()))+" lower")
return False
else:
printcopying=pyperclip.copy(value)
print(str(int(copying.paste()))+" correct guess our number was "+ str(int(value)))
return True
print("lets play a guessing game, can you get our number? type in your guess")
userAnswer=int(input())
while(guess(userAnswer)==False):
print("lets play a guessing game, can you get our number? type in your guess")
userAnswer
guess(userAnswer)
解决方案
推荐阅读
- java - JavaFX获取旋转的imageView的角坐标?
- python - Python更改现有Windows快捷方式的图标
- sentry - 哨兵报错时如何查找线路?
- javascript - 你如何将数据从一个字段复制到另一个字段并在javascript中附加一个文本字符串
- reactjs - 无法更新反应组件中的状态
- ruby - 将散列动态添加到空数组
- windows - 无法附加到批处理文件中的环境变量
- ruby-on-rails - Rails:按参数搜索不起作用 - 关联问题?
- linux - 双启动 Windows 7 和 CentOS 7,缺少 Windows 7 BOOTMGR
- python - 如何取消填充 PKCS#7 / PKCS#5 填充?