试图用python在rock Paper和scissor上编写代码

试图用python在rock Paper和scissor上编写代码,python,Python,在输出时,我得到以下错误: import random as rand import string string.letters='rps' comp = rand.choice(string.letters) user = input("enter r for rock,p for paper,s for scissors\n") print(comp) if comp==user: print("its a tie") elif comp==r and user==p: pr

在输出时,我得到以下错误:

import random as rand
import string
string.letters='rps'
comp = rand.choice(string.letters)
user = input("enter r for rock,p for paper,s for scissors\n")
print(comp)
if comp==user:
    print("its a tie")
elif comp==r and user==p:
    print("  congrats you  won ")
elif comp==r and user==s:
    print("sorry you lost")
elif comp==p and user==r:
    print("sorry you lost") 
elif comp==p and user==s:
    print("  congrats you  won ")
elif comp==s and user==p:
    print("sorry you lost")
elif comp==s and user==r:
    print("  congrats you  won ")
输入r表示岩石,p表示纸张,s表示剪刀
s
R
回溯(最近一次呼叫最后一次):
文件“rpcs.py”,第12行,在
elif comp==r和user==p:
NameError:未定义名称“r”

任何帮助都很好,谢谢你

'r'
替换像
r
这样的东西。Python正在读取您作为名为
r
的变量编写的
r
,并将其视为字符串。对
p
s
等执行相同的操作…

r
等替换为
'r'
。Python正在读取您作为名为
r
的变量编写的
r
,并将其视为字符串。对
p
s
等执行相同的操作…

您可以这样尝试:

enter r for rock,p for paper,s for scissors
s
r
Traceback (most recent call last):
   File "rpcs.py", line 12, in <module>
    elif comp==r and user==p:
NameError: name 'r' is not defined
说明: 字符需要与
引号一起使用
。否则python会将其作为变量。由于未使用引号,因此引发错误
name错误:未定义名称“r”

您可以这样尝试:

enter r for rock,p for paper,s for scissors
s
r
Traceback (most recent call last):
   File "rpcs.py", line 12, in <module>
    elif comp==r and user==p:
NameError: name 'r' is not defined
说明:
字符需要与
引号一起使用
。否则python会将其作为变量。由于未使用引号,因此引发错误
NameError:name'r'未定义

这无法工作,因为在执行条件测试时,您没有输入字符串。以下是一个示例:

import random as rand
import string
string.letters='rps'
comp = rand.choice(string.letters)
user = input("enter r for rock,p for paper,s for scissors\n")
print(comp)
if comp==user:
    print("its a tie")
elif comp == 'r' and user == 'p':
    print("  congrats you  won ")
elif comp == 'r' and user == 's':
    print("sorry you lost")
elif comp == 'p' and user == 'r':
    print("sorry you lost") 
elif comp == 'p' and user == 's':
    print("congrats you  won")
elif comp == 's' and user == 'p':
    print("sorry you lost")
elif comp == 's' and user == 'r':
   print("congrats you  won")

请注意,在每个条件测试中,我们将要比较的值作为字符串输入。如果它们不是字符串对象,则在此示例中无法进行比较。简单地放置p、r或s将不起作用,但它们需要用引号括起来,如您所见。否则,将出现回溯错误

这无法工作,因为在执行条件测试时,您没有输入字符串。以下是一个示例:

import random as rand
import string
string.letters='rps'
comp = rand.choice(string.letters)
user = input("enter r for rock,p for paper,s for scissors\n")
print(comp)
if comp==user:
    print("its a tie")
elif comp == 'r' and user == 'p':
    print("  congrats you  won ")
elif comp == 'r' and user == 's':
    print("sorry you lost")
elif comp == 'p' and user == 'r':
    print("sorry you lost") 
elif comp == 'p' and user == 's':
    print("congrats you  won")
elif comp == 's' and user == 'p':
    print("sorry you lost")
elif comp == 's' and user == 'r':
   print("congrats you  won")

请注意,在每个条件测试中,我们将要比较的值作为字符串输入。如果它们不是字符串对象,则在此示例中无法进行比较。简单地放置p、r或s将不起作用,但它们需要用引号括起来,如您所见。否则,将出现回溯错误

r
放在单引号“r”中。在python中,字符应该用引号括起来。否则,它将被视为新的变量put
r
在单引号“r”中。在python中,字符应该用引号括起来。否则,它将被视为新变量