您好,这是我再次询问关于我的代码的问题我正在创建一个石头,纸,剪刀我添加了main()
和def main():
,并缩进了代码,但当我运行代码时发生了这种情况
C:\Users\Timothy\Documents>python text.py
C:\Users\Timothy\Documents>
这是我的代码
def main():
import random
print("1=Rock, 2=Paper, 3=Scissor")
player = int(input("Please Put your choice"))
ai = random.randint(1,3)
###if and else statement for ai to print the random integer to string
if (ai == 3):
print("AI: Scissor")
elif (ai == 2):
print("AI: Paper")
elif (ai == 1):
print("AI: Rock")
####if and statement for the ai and player
if (player == ai):
print("It is a tie")
if (player == 1 and ai == 2):
print("AI won")
if (player == 1 and ai == 3):
print("You Won!!")
if (player == 2 and ai == 1):
print("You won")
if (player == 2 and ai == 3):
print("AI won")
if (player == 3 and ai == 1):
print("AI won")
if (player == 3 and ai == 2):
print("You won")
main()
发布于 2019-09-30 14:51:42
下面是一种使用while True
循环程序的方法,这样游戏就可以多次运行。
def main():
import random
print("1=Rock, 2=Paper, 3=Scissor")
player = int(input("Please Put your choice"))
ai = random.randint(1,3)
###if and else statement for ai to print the random integer to string
if (ai == 3):
print("AI: Scissor")
elif (ai == 2):
print("AI: Paper")
elif (ai == 1):
print("AI: Rock")
####if and statement for the ai and player
if (player == ai):
print("It is a tie")
if (player == 1 and ai == 2):
print("AI won")
if (player == 1 and ai == 3):
print("You Won!!")
if (player == 2 and ai == 1):
print("You won")
if (player == 2 and ai == 3):
print("AI won")
if (player == 3 and ai == 1):
print("AI won")
if (player == 3 and ai == 2):
print("You won")
while True:
main()
restart = input("Again? Y/N: ").upper()
if restart == "Y":
main()
if restart == "N":
break
发布于 2019-09-30 14:45:38
是的,缩进得太多了。
def main():
import random
print("1=Rock, 2=Paper, 3=Scissor")
player = int(input("Please Put your choice"))
ai = random.randint(1,3)
###if and else statement for ai to print the random integer to string
if (ai == 3):
print("AI: Scissor")
elif (ai == 2):
print("AI: Paper")
elif (ai == 1):
print("AI: Rock")
####if and statement for the ai and player
if (player == ai):
print("It is a tie")
if (player == 1 and ai == 2):
print("AI won")
if (player == 1 and ai == 3):
print("You Won!!")
if (player == 2 and ai == 1):
print("You won")
if (player == 2 and ai == 3):
print("AI won")
if (player == 3 and ai == 1):
print("AI won")
if (player == 3 and ai == 2):
print("You won")
main() # this way if will run if this condition is met player == 3 and ai == 2
main() # this will surely run in the end calling itself
https://stackoverflow.com/questions/58162652
复制相似问题