【问题标题】:Searching for a certain word decided by a .format in a print()在 print() 中搜索由 .format 决定的某个单词
【发布时间】:2020-05-21 23:10:34
【问题描述】:

我是 python 新手,有人告诉我一个好的起点是编写一个石头剪刀布程序。 这是我到目前为止所拥有的:

import random

term1 = "Rock"
term2 = "Paper"
term3 = "Scissors"
computerchose = [term1, term2, term3]

print("Welcome to Rock Paper Scissors!")
print("You will be versing the computer, Good luck!")
print("Rock \nPaper \nScissors")
input = input(print("please choose your weapon "))
words = input.split()
if term1 in words:
    print("Oh you chose Rock did you?!? Well the computer chose {0}".format(random.choice(computerchose)))

if term2 in words:
    print("Oh you chose Paper did you?!? Well the computer chose {0}".format(random.choice(computerchose)))

if term3 in words:
    print("Oh you chose Scissors did you?!? Well he computer chose {0}".format(random.choice(computerchose)))

我希望尝试创造一个获胜条件。我对此的思考过程是获得 {0}.format 答案并分别检查它的 term1、term2、term3。如果有人能帮我解决这个问题,我会非常感激!

【问题讨论】:

  • 你的问题是什么?
  • 为什么在对input()的调用中有print()?
  • 为什么需要拆分输入?您一次只能选择一种武器,包括石头、纸、剪刀。
  • 请看this answer。希望您能对如何实现游戏有所了解。

标签: python python-2.7


【解决方案1】:

你应该给电脑的选择设置一次变量,然后和玩家的选择进行比较。

humanchoice = input("Please choose your weapon")
computerchoice = random.choice(computerchose)
if humanchoice == term1:
    if computerchoice == term2:
        winner = "computer"
    elif computerchoice == term3:
        winner = "human"
    else: winner = "tie"
elif humanchoice == term2:
    if computerchoice == term3:
        winner = "computer"
    elif computerchoice == term1:
        winner = "human"
    else:
        winner = "tie"
elif humanchoice == term3:
    if computerchoice == term1:
        winner = "computer"
    elif computerchoice == term2:
        winner = "human"
    else:
        winner = "tie"
else:
    print("That's not a valid weapon")

【讨论】:

    猜你喜欢
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2021-06-22
    • 2011-04-21
    • 2015-05-22
    相关资源
    最近更新 更多