' not supported between instances of ' int ' and ' 无类型 '"-6ren"> ' not supported between instances of ' int ' and ' 无类型 '"-这个问题已经有答案了: What is the purpose of the return statement? How is it different from printing? (15 个回答)-6ren">
gpt4 book ai didi

python - "TypeError: ' > ' not supported between instances of ' int ' and ' 无类型 '"

转载 作者:太空宇宙 更新时间:2023-11-03 19:42:17 26 4
gpt4 key购买 nike

我正在使用 def 函数,当我运行代码时,我收到“TypeError:‘int’和‘NoneType’实例之间不支持‘>’”。我明白了错误的意思,但是有什么方法可以将非类型更改为 int 吗?

import random 
random.randint(1,3)

def P1():
print("pick ")
playerinput = int(input("rock(1), paper(2), scissors(3):"))

if playerinput == 1:
print("Player 1 chose: rock")
elif playerinput == 2:
print("Player 1 chose: paper")
elif playerinput == 3:
print("Player 1 chose: scissors")
return (playerinput)

def P2():
computer = random.randint(1,3)
if computer == 1:
print("CPU chose: rock")
elif computer == 2:
print("CPU chose: paper")
elif computer == 3:
print("CPU 2 chose: scissors")

def winner():
if P1()>P2():
print("Player 1 wins")


def main():
winner()

main()

最佳答案

if P1()>P2(): 此处发生错误

在 Python 中,所有函数都隐式返回 None,并且 return 语句可用于给出有用的值

def f():
pass

调用时返回None

def f():
return 1

调用时返回1

请注意,您的函数 P2 没有 return 语句,因此调用它将导致 None。正如您的错误消息所述, int 之间不支持 > (大于运算符,您与 P2() 的结果一起使用)和NoneType。一般来说,除了 == 之外,None 不具有可比性,但在确定值是否为 None 时,使用 is 被认为更好

What is the difference between " is None " and " ==None "

关于python - "TypeError: ' > ' not supported between instances of ' int ' and ' 无类型 '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60358153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com