我正在做一个非常基本的石头剪刀游戏,它必须在功能上,但也有一些问题


import random
import time
def intro():
name = input("Please enter your name: ")
print(f" Hello, {name} today we are going to be playing Rock, Paper, Scissors.")

def game():
options = ["rock", "paper", "scissors"]
print("What do you want: rock, paper, scissors?")
answer = input()
while answer not in options:
print("Invalid choice. Please pick another option,")
answer = input()
def process():
bot = random.choice(options)
print("Bot:"+ bot)
print("User:" + answer)

def winner():
if bot == answer:
print("Tie")
elif answer == "rock":
if bot == "paper":
print("Paper covers rock so computer wins")
else: 
print("Rock smashes scissors, so you win.")
elif answer == "paper":
if bot == "rock":
print("Paper covers rock user wins")
else: 
print("Scissorsr cuts paper so bot wins")
else: 
if bot == "paper":
print("scissors cuts paper so user wins")
else:
print("rock smashes scissors so bot wins") 

intro()
game()
process()
winner()

我正在制作一个非常基本的石头剪刀游戏,它必须有功能,但也有一些问题,它一直存在压痕问题和其他问题。我需要帮助修复代码。有人能把这个代码放在repl.it上运行,然后帮我解决它吗。非常感谢。

您还可以使用全局变量。

import random
import time
options = ["rock", "paper", "scissors"]
answer = ""
bot = ""
def intro():
name = input("Please enter your name: ")
print(f" Hello, {name} today we are going to be playing Rock, Paper, Scissors.")

def game():
print("What do you want: rock, paper, scissors?")
global answer
answer = input()
while answer not in options:
print("Invalid choice. Please pick another option,")
answer = input()
def process():
global answer, bot
bot = random.choice(options)
print("Bot:"+ bot)
print("User:" + answer)

def winner():
if bot == answer:
print("Tie")
elif answer == "rock":
if bot == "paper":
print("Paper covers rock so computer wins")
else: 
print("Rock smashes scissors, so you win.")
elif answer == "paper":
if bot == "rock":
print("Paper covers rock user wins")
else: 
print("Scissorsr cuts paper so bot wins")
else: 
if bot == "paper":
print("scissors cuts paper so user wins")
else:
print("rock smashes scissors so bot wins") 

intro()
game()
process()
winner()

函数应该达到目的并返回值,尤其是因为在未来的函数中需要过去的值。

import random
import time
options = ["rock", "paper", "scissors"]
def intro():
name = input("Please enter your name: ")
return name

def game():
print("What do you want: rock, paper, scissors?")
answer = input()
while answer not in options:
print("Invalid choice. Please pick another option,")
answer = input()
return answer
def process():
bot = random.choice(options)
return bot

def winner(bot, answer):
if bot == answer:
return "Tie"
elif answer == "rock":
if bot == "paper":
return "Paper covers rock so computer wins"
else:
return "Rock smashes scissors, so you win."
elif answer == "paper":
if bot == "rock":
return "Paper covers rock user wins"
else:
return "Scissorsr cuts paper so bot wins"
else:
if bot == "paper":
return "scissors cuts paper so user wins"
else:
return "rock smashes scissors so bot wins"

name = intro()
print( f" Hello, {name} today we are going to be playing Rock, Paper, Scissors.")
answer = game()
bot = process()
print("Bot:", bot)
print("User:" , answer)
print( winner( bot, answer ) )

您应该将变量作为参数传递给函数:

import random
import time
def intro():
name = input("Please enter your name: ")
print(f" Hello, {name} today we are going to be playing Rock, Paper, Scissors.")
def game():
options = ["rock", "paper", "scissors"]
print("What do you want: rock, paper, scissors?")
answer = input()
while answer not in options:
print("Invalid choice. Please pick another option,")
answer = input()
return options,answer
def process(options,answer):
bot = random.choice(options)
print("Bot:"+ bot)
print("User:" + answer)
return bot
def winner(answer,bot):
if bot == answer:
print("Tie")
elif answer == "rock":
if bot == "paper":
print("Paper covers rock so computer wins")
else: 
print("Rock smashes scissors, so you win.")
elif answer == "paper":
if bot == "rock":
print("Paper covers rock user wins")
else: 
print("Scissorsr cuts paper so bot wins")
else: 
if bot == "paper":
print("scissors cuts paper so user wins")
else:
print("rock smashes scissors so bot wins")
intro()
a,b=game()
c=process(a,b)
winner(b,c)

global的示例(尽管不推荐(:

import random
import time
def intro():
name = input("Please enter your name: ")
print(f" Hello, {name} today we are going to be playing Rock, Paper, Scissors.")
def game():
global options
global answer
options = ["rock", "paper", "scissors"]
print("What do you want: rock, paper, scissors?")
answer = input()
while answer not in options:
print("Invalid choice. Please pick another option,")
answer = input()

def process():
global bot
bot = random.choice(options)
print("Bot:"+ bot)
print("User:" + answer)

def winner():
if bot == answer:
print("Tie")
elif answer == "rock":
if bot == "paper":
print("Paper covers rock so computer wins")
else: 
print("Rock smashes scissors, so you win.")
elif answer == "paper":
if bot == "rock":
print("Paper covers rock user wins")
else: 
print("Scissorsr cuts paper so bot wins")
else: 
if bot == "paper":
print("scissors cuts paper so user wins")
else:
print("rock smashes scissors so bot wins")
intro()
game()
process()
winner()

最新更新