【发布时间】:2015-11-15 08:15:41
【问题描述】:
我正在尝试用 Python 制作一个 Heads Or Tails 程序。我是一个新手,我刚刚接触到 Python。我试图实现的是让程序在我不知道的情况下选择 Heads 或 Tails(是的,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”之类的东西,有错误,程序会告诉他他的答案有错误。