首页 > 解决方案 > 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)

标签: python

解决方案


推荐阅读