мои ножницы-камень, бумага ничего не дают мне на выходе (python)

#python #windows #function #random #output

#python #Windows #функция #Случайный #вывод

Вопрос:

Я новичок в python, и в качестве практики я пытаюсь написать на нем rock paper scissors. Я трижды проверил eth, но не могу найти проблему. Я использую консоль Visual Studio code. msvcrt предназначен для «getch», и я не уверен в синтаксисе случайной функции

проблема: когда вы даете ему номер ввода, он ничего не делает и не записывает, несмотря на программу. помогите мне, пожалуйста.

 import random
import msvcrt 
##################################################
player_move = " "
system_move = " "
##################################################
def rand(system_move):
    rn = random.randint(1, 3)
    if rn == 1:
        system_move = "Rock"
    elif rn == 2:
        system_move = "Paper"
    elif rn == 3:
        system_move = "Scissors"
    else:
        rand()
    return system_move
##################################################
print("!!! Rock, Paper, Scissors !!!nnn")
###################################################
def playermove(player_move):
    print("What do you want to go with?n1-Rockn2-papern3-scissorsn")
    temp = msvcrt.getch()
    if temp == '1' or 1:
        player_move = 'Rock'
    elif temp == '2' or 2:
        player_move = 'Paper'
    elif temp == '3' or 3:
        player_move = 'Scissors'
    else:
        print(f"invalid input {player_move}. Try againn")
        playermove()
    return player_move
###################################################
pm = ' '
sm = ' '
rand(sm)
playermove(pm)
if pm== 'Scissors':
    if sm == "Scissors":
        print(f"System move: {sm}nIt's a tie!n")
    elif sm == "Rock":
        print(f"System move: {sm}nSystem won!n")
    elif sm == "Paper":
        print(f"System move: {sm}nYou won!n")
    else:
        print("Oops! Something went wrong.n")
        
elif pm == "Paper":
    if sm == "Scissors":
        print(f"System move: {sm}nSystem won!n")
    elif sm == "Rock":
        print(f"System move: {sm}nYou won!n")
    elif sm == "Paper":
        print(f"System move: {sm}nIt's a tie!n")
    else:
        print("Oops! Something went wrong.n")
elif pm == "Rock":
    if sm == "Scissors":
        print(f"System move: {sm}nYou won!n")
    elif sm == "Rock":
        print(f"System move: {sm}nIt's a tie!n")
    elif sm == "Paper":
        print(f"System move: {sm}nSystem won!n")
    else:
        print("Oops! Something went wrong.n")

print("Press any key to exit...")
xyz = msvcrt.getch()
  

Комментарии:

1. Почему вы не используете input функцию для получения входных данных от пользователя?

2. потому что вам нужно будет нажать enter с вводом

Ответ №1:

Ну, причина, по которой ваш код не работает, заключается в том, что вы не присваиваете возвращаемое значение ваших функций какой-либо переменной. Чтобы исправить ваш код, вам нужно выполнить следующее:

 sm = rand(sm)
pm = playermove(pm)
  

В Python передача неизменяемого объекта, такого как string, означает, что вы не можете вносить в него какие-либо изменения. Поскольку вы не используете переданное значение, сигнатура вашей функции должна выглядеть примерно так.

 def rand()
def playermove()
  

После того, как вы сделаете это, вы увидите, что в вашем коде есть другие вещи, которые вам нужно исправить.

Комментарии:

1. Нет проблем. Если решение помогло вам, отметьте его как принятый ответ.

Ответ №2:

Попробуйте этот код. Просто замените snake, water, gun на rock, paper, scissors .

 
print("  ttt Welecome to Snake , Water , Gun gamenn")


import random

attempts= 1
your_point=0
computer_point=0
while (attempts<=10):

    lst=["snake","water","gun"]
    ran=random.choice(lst)
    print("what do you choose snake, water or gun")
    inp=input()


    if inp==ran:
        print("tie")
        print(f"you choosed {inp} and computer guess is {ran}")
        print("No body gets pointn")



    elif inp=="snake" and ran=="water":
        your_point=your_point 1
        print(f"you choosed {inp} and computer guess is {ran}")
        print("The snake drank watern")
        print("You won this round")
        print("you get 1 pointn")


    elif inp=="water" and ran=="snake":
        computer_point = computer_point   1
        print(f"you choosed {inp} and computer guess is {ran}")
        print("The snake drank watern")
        print("You Lost this round")
        print("computer gets 1 pointn")

    elif inp=="water" and ran=="gun":
        print(f"you choosed {inp} and computer guess is {ran}")
        your_point = your_point   1
        print("The gun sank in watern")
        print("You won this round")
        print("you get 1 pointn")


    elif inp == "gun" and ran == "water":
        print(f"you choosed {inp} and computer guess is {ran}")
        computer_point = computer_point   1
        print("The gun sank in watern")
        print("You Lost this round")
        print("computer gets 1 pointn")



    elif inp == "gun" and ran == "snake":
        print(f"you choosed {inp} and computer guess is {ran}")
        your_point = your_point   1
        print("The snake was shoot and he diedn")
        print("You Won this round")
        print("you get 1 pointn")

    elif inp == "snake" and ran == "gun":
        print(f"you choosed {inp} and computer guess is {ran}")
        computer_point=computer_point 1
        print("The snake was shoot and he diedn")
        print("You Lost this round")
        print("computer gets 1 pointn")

    else:
        print("invalid")




    print(10 - attempts, "no. of guesses left")
    attempts = attempts   1

    if attempts>10:
        print("game over")

if computer_point > your_point:
    print("Computer wins and you loose")

if computer_point < your_point:
    print("you win and computer loose")

print(f"your point is {your_point} and computer point is {computer_point}")