简体   繁体   English

使用变量定义字符串,然后使用该变量作为输入

[英]Defining a string using a variable, then using that variable as an imput

I am new to this site, so please bear with me.我是这个网站的新手,所以请耐心等待。 I need to create a rock paper scissors program that outputs each round and the winner of each round, but am having difficulties with the inputs.我需要创建一个石头剪刀布程序,输出每一轮和每轮的获胜者,但在输入方面遇到困难。 When I run my program (this is only a section of it), the computer is able to recognize that R=rock, P=paper, and S=scissors, however when R, P, or S is input in PlayerChoice, the program does not understand that R stands for rock or P stands for Paper, etc. How can I fix this problem?当我运行我的程序时(这只是它的一部分),计算机能够识别出 R=rock、P=paper 和 S=scissors,但是当在 PlayerChoice 中输入 R、P 或 S 时,程序不明白 R 代表岩石或 P 代表纸等。我该如何解决这个问题?

R='rock'
P='paper'
S='scissors'
RPS=[R,P,S]
Name=input('please enter your name')

while PlayerCounter<3 and CompCounter<3

PlayerChoice=input('choose:rock(R),paper(P),scissors(S)')

CompChoice=RPS[random.randint(0,2)]

print('computer chose ' + str(CompChoice))

this is a sample of the output这是输出的示例

__________Game  1 __________
choose:rock(R),paper(P),scissors(S) R
computer chose rock
__________Game  1 __________
choose:rock(R),paper(P),scissors(S)P
computer chose rock
__________Game  1 __________
choose:rock(R),paper(P),scissors(S)S
computer chose scissors

"Understand" is a matter of programming. “理解”是编程的问题。 You have to use the same representation for both players.您必须对两个玩家使用相同的表示。 The problem here is that you used the characters 'R', 'P', 'S' for the player, but integers 0, 1, 2 for the computer.这里的问题是你对玩家使用了字符 'R'、'P'、'S',而对计算机使用了整数 0、1、2。 Try this:尝试这个:

move_dict = {'R': 0, 'P': 1, 'S': 2}
player_num = move_dict[PlayerChoice]
move_diff = CompChoice - player_num
# Now you evaluate move_diff to find out who wins.

I recommend that you convert the player choice to the same integers set.我建议您将玩家选择转换为相同的整数集。 Then you can simply subtract the two choices to find out who won: 0 is a tie, 1 or -2 is one side;然后你可以简单地减去两个选项来找出谁赢了:0 是平局,1 或 -2 是一侧; 2 or -1 is the other side. 2 或 -1 是另一边。 You can use the modulus operation "% 3" to map that to just 0, 1, 2 as a game result.您可以使用模运算“% 3”将其映射到 0、1、2 作为游戏结果。

Does that get you moving?这能让你动起来吗?

It would be much easier to use a single list to handle the moves of the player and the computer in this case.在这种情况下,使用单个列表来处理玩家和计算机的移动会容易得多。 The transition from the strings to characters is an unnecessary complication.从字符串到字符的转换是不必要的复杂化。

import random

# Establish the potential values from which players can choose
values = ["R", "P", "S"]

chosen = 0
# Don't proceed until the user has provided reasonable input
while chosen == 0:
    # The [0] at the end of this finds the first letter, even if the player inputs something like "rock"
    player_choice = input("Choose rock (R), paper (P), or scissors (S): ").upper()[0]
    if player_choice in values:
        chosen = 1 # Allow the program to proceed

# Choose a random value for the computer
computer_choice = values[random.randint(0, 2)]

# Display the results
print("The computer chose \"" + computer_choice + "\" and the player chose \"" + player_choice + "\"")

However, I agree with Prune's response: you should use integers in place of characters or strings, else you'll have some complicated logic to determine the winner of the game (paper beats rock, rock beats scissors, scissors beats paper, rock is beaten by paper, scissors are beaten by rock, paper is beaten by scissors).但是,我同意 Prune 的回答:您应该使用整数代替字符或字符串,否则您将有一些复杂的逻辑来确定游戏的获胜者(纸打败石头,石头打败剪刀,剪刀打败纸,石头被打败被纸打,剪刀被石头打,纸被剪刀打)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM