【问题标题】:Python Heads Or Tails Program assistancePython Heads or Tails 计划协助
【发布时间】:2015-11-15 08:15:41
【问题描述】:

我正在尝试用 Python 制作一个 Heads Or Tails 程序。我是一个新手,我刚刚接触到 Python。我试图实现的是让程序在我不知道的情况下选择 HeadsTails(是的,import random 等),我想要一个猜的时候单试。这是我到目前为止所取得的成就,但它与我正在寻找的东西并不十分接近。有什么想法吗?我已经尝试实现我在 Python 网站上找到的不同随机参数,但它们不起作用(例如 randint 用于整数)...谢谢!

print """
Welcome to our game. This is a heads or tails game and you will be the one who gets to pick a possible answer. Lets begin!
"""

print "~-~-~-~-" * 10

theirGuess = raw_input("Whats your guess? :   ")
ourAnswer = "Heads"   # For Debugging purposes to check if the program works
notCorrectAnswer = "Tails"  # To eliminate the possibility of not being either tails or heads in case of mistaken answer 


if theirGuess == ourAnswer:
      print "You got it!"
elif theirGuess != notCorrectAnswer and ourAnswer:
    print "You didnt get it! Try entering either Tails or Heads!"
else:
    print "You didnt get it! Try again next time!"

【问题讨论】:

  • elif theirGuess != notCorrectAnswer and ourAnswer: 应该是 elif theirGuess != notCorrectAnswer。我假设您的意思是 elif theirGuess != ourAnswer and theirGuess != notCorrectAnswer,但您已经通过 elif 子句确定了 theirGuess != ourAnswer
  • 我想写的是,如果“玩家”输入了诸如“Tais”之类的东西,有错误,程序会告诉他他的答案有错误。

标签: python random


【解决方案1】:

你应该试试:

import random
ch = random.choice(["Heads","Tails"])

这会将“Heads”或“Tails”放入变量ch。尝试从中做点什么。

【讨论】:

  • 这就像一个魅力。但是,我想继续循环浏览该程序。简而言之,我希望玩家继续玩游戏直到他们退出自己,而不必再次启动“应用程序”。这是我的方法。不工作。我怎么能改变它?对不起,很长的问题:/
  • def second(): restart = raw_input("你想重启这个程序再试一次吗?") y = "yes" n = "no" if restart == y: main () elif restart == n: sys.exit(0)
  • 您也可以将整个内容放入 while 循环中,并附加一个 raw_input() 输入。
  • 问题是,它不起作用。有什么问题吗?
【解决方案2】:

为了让整个事情一直持续到用户退出,包括@Baruchel 的回答:

import random
print """
Welcome to our game. This is a heads or tails game and you will be the one who gets to pick a possible answer. Lets begin!
"""
cont = 1 #To force the game to run for one round without user input
while(cont == 1): #cont is used to take user choice, whether to run it again or not
    print "~-~-~-~-" * 10


    theirGuess = raw_input("Whats your guess? :   ")
    ourAnswer = random.choice(["Heads","Tails"])
    if ourAnswer == "Heads":
        notCorrectAnswer = "Tails"
    else:
        notCorrectAnswer = "Heads"


    if theirGuess == ourAnswer:
        print "You got it!"
    elif theirGuess != notCorrectAnswer and ourAnswer:
        print "You didnt get it! Try entering either Tails or Heads!"
    else:
        print "You didnt get it! Try again next time!"

    cont = input("Do you want to continue? Press 1 if yes, press 0 if no.: ") #Take user choice on whether to run it again or not
    while (cont != 0 and cont != 1): # If user puts in a different number (neither 1 or 0), make them enter until they put a right choice
        cont = input("Please try again. Press 1 if yes, press 0 if no.: ")

【讨论】:

  • 总结了一下。不太明白为什么将 cont 设置为 1,然后将其设置为输入,然后设置为另一个输入,但它起作用了。感谢您的帮助!
  • 我将cont 设置为 1 以第一次运行循环(即玩第一轮游戏)。然后由用户选择是否再次播放。但万一他们不小心输入了 1 或 0 以外的东西,我让他们再次输入一个数字。理想情况下,该部分应该在一个循环中,以便每次用户输入错误的数字(即,不是 0 或 1)时,都会提示他/她。
猜你喜欢
  • 2022-06-16
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2019-10-26
相关资源
最近更新 更多