我的石头剪刀在输出中什么也没给我(python)



我是python的新手,作为练习,我正尝试用它编写一把岩石剪刀。我查了三遍,但没有发现问题。我正在使用visualstudio代码控制台。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()

好吧,您的代码不起作用的原因是您没有将函数的返回值分配给任何变量。要修复您的代码,您需要执行以下操作:

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

在Python中,传递一个像字符串这样的不可变对象意味着你不能对它进行任何更改。由于你没有使用传递的值,你的函数签名实际上应该是这样的。

def rand()
def playermove()

完成此操作后,您将看到代码中还有其他需要修复的内容。

试试这个代码。只要用石头、纸、剪刀代替蛇、水、枪。


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}")

最新更新