Я создал игру Rock Paper Scissors. Он спрашивает вас, хотите ли вы играть, принимает ваш вход, сравнивает его со случайным вводом компьютера, дает результат, а затем спрашивает, хотите ли вы снова играть.
Я не знаю, почему он не работает. Пожалуйста, дайте мне знать, как это исправить. Благодарю вас
import random
import sys
#play
def play():
print('want to play?')
if input()=='yes':
print("Pick R/P/S")
user = input()
# RPS
def RPS():
x = random.choice( ['Rock', 'Paper', 'scissors'] )
print(x)
if x == 'Rock' and user == 'R':
print('Tie')
elif x == 'Rock' and user == 'P':
print('You beat Comp')
elif x == 'Rock' and user == 'S':
print('Beat you')
#--------
if x == 'Paper' and user == 'R':
print('Lost to comp')
elif x == 'Paper' and user == 'P':
print('tie')
elif x == 'Paper' and user == 'S':
print('you beat comp')
#---------
if x == 'scissors' and user == 'R':
print('Beat comp')
elif x == 'scissors' and user == 'P':
print('lost comp')
elif x == 'scissors' and user == 'S':
print('tie')
#Play again
def playagain():
print('Want to play again')
play=input()
if play=='yes':
RPS()
else:
print("Thanks for playing")
play()
RPS()
playagain()
Попробуйте это, это должно сработать. Я только что редактировал код, чтобы сделать его немного лучше:
import random
import sys
con_1 = True
ask_to_play = input('want to play?:')
if ask_to_play=='yes':
while con_1:
print("Pick R/P/S")
user = input()
x = random.choice( ['Rock', 'Paper', 'scissors'] )
print(x)
if x == 'Rock' and user == 'R':
print('Tie')
elif x == 'Rock' and user == 'P':
print('You beat Comp')
elif x == 'Rock' and user == 'S':
print('Beat you')
#--------
if x == 'Paper' and user == 'R':
print('Lost to comp')
elif x == 'Paper' and user == 'P':
print('tie')
elif x == 'Paper' and user == 'S':
print('you beat comp')
#---------
if x == 'scissors' and user == 'R':
print('Beat comp')
elif x == 'scissors' and user == 'P':
print('lost comp')
elif x == 'scissors' and user == 'S':
print('tie')
print('Want to play again(Y/n)?:')
play=input()
if play=='Y' or play == "y":
con_1 = True
else:
print("Thanks for playing")
con_1 = False
else:
print("I hope you come back to play again.")
У вас были проблемы с углублением. Также вам нужно объявить переменную user
в глобальной области. Посмотрите на этот код:
import random
import sys
user = None
def play():
print('want to play?')
if input()=='yes':
print("Pick R/P/S")
global user
user = input()
def RPS():
x = random.choice( ['Rock', 'Paper', 'scissors'] )
print(x)
if x == 'Rock' and user == 'R':
print('Tie')
elif x == 'Rock' and user == 'P':
print('You beat Comp')
elif x == 'Rock' and user == 'S':
print('Beat you')
#--------
if x == 'Paper' and user == 'R':
print('Lost to comp')
elif x == 'Paper' and user == 'P':
print('tie')
elif x == 'Paper' and user == 'S':
print('you beat comp')
#---------
if x == 'scissors' and user == 'R':
print('Beat comp')
elif x == 'scissors' and user == 'P':
print('lost comp')
elif x == 'scissors' and user == 'S':
print('tie')
def playagain():
print('Want to play again')
play=input()
if play=='yes':
RPS()
else:
print("Thanks for playing")
play()
RPS()
playagain()