所以我想开发游戏和mod等等,所以我开始研究编程语言并决定选择python作为我的起始语言,因为它看起来相当简单而且非常有效。我已经使用了大约两天,并且利用我目前的知识,我试着为猜谜游戏编写代码!在您继续阅读之前,这是我游戏的代码:
import random
print("Welcome to a guessing game!")
print ("You have 3 try's to guess the correct number!")
number = raw_input ("Pick a number between 1 and 10!: " )
compnumber = random.randint (1,10)
enter = "Please press enter to exit..."
winner = "Congrats your a Winner!"
loser_high = "Uh-Oh! That Number is to high! Guess again!"
loser_low = "Uh-Oh! That number is to low! Guess again!"
demand = "Pick a number between 1 and 10!: "
game_over = "Sorry, you have lost the game!"
#Guess Number One
if number == compnumber :
print (winner)
elif number != compnumber and number > compnumber :
print (loser_high)
guess2 = raw_input ()
elif number != compnumber and number < compnumber :
print (loser_low)
guess2 = raw_input (demand)
#Guess Number Two
if guess2 == compnumber :
print (winner)
elif guess2 != compnumber and guess2 > compnumber :
print (loser_high)
elif guess2 != compnumber and guess2 < compnumber :
print (loser_low)
guess3 = raw_input (demand)
#Guess Number Three
if guess3 == compnumber :
print (winner)
elif guess3 != compnumber and guess3 > compnumber :
print (loser_high)
elif guess3 != compnumber and guess3 < compnumber :
它只是一个简单的40行代码,我为此感到非常自豪:D
所以无论如何这是我的问题。当我运行应用程序时,我没有得到任何语法错误或一般的任何错误但是当我输入我的第一个猜测它总是说我的猜测是高...当我再次尝试它说同样的事情然后控制台关闭......这是为什么?
另外,如果有人能就如何简化/压缩我当前的代码给我一些建议,请在下面发布,如果有人有一些学习更高级python的教程,请告诉我!感谢!!!
答案 0 :(得分:2)
您的问题是raw_input()
返回一个字符串,random.randint()
返回一个整数。在Python 2中,比较不同类型的对象是合法的,并且字符串总是比整数更高。
您需要将用户输入转换为int
,然后它才能正常工作。
guess = int(raw_input())
此外,您不应该通过复制/粘贴三次或多或少相同的代码来实现循环。使用for
或while
循环:
for attempt in range(3):
guess = int(raw_input(demand))
if guess == compnumber:
print winner
break # abort the loop
elif guess > compnumber:
print loser_high
else:
print loser_low
else:
print game_over
答案 1 :(得分:2)
我怀疑问题是原始输入是一个字符串,但随机值是一个整数。尝试将第7行更改为:
number = int(raw_input("Pick a number between 1 and 10!: ").strip())
这个修改做了两件事:
.strip()
)int()
)您还需要在用户输入的其他行上执行类似的操作。
答案 2 :(得分:2)
您需要将raw_input
返回的字符串转换为数字。您还可以为整个游戏设置一个循环,并防止代码重复。另一件事是,在number == compnumber
条件中检查if
后,您不必在其他部分检查number != compnumber
:
# ....
for i in range(3):
number = int(raw_input ("Pick a number between 1 and 10!: " ))
if number == compnumber :
print (winner)
exit(0)
elif number > compnumber :
print (loser_high)
elif number < compnumber :
print (loser_low)
print "Guess again!"
答案 3 :(得分:-1)
import random
LOSER_HIGH, LOSER_LOW, WINNER = 1, -1, 0
GUESS_TIMES = 3
WIN_STR = "Congrats your a Winner!"
GAMEOVER_STR = "Sorry, you have lost the game!"
LOSER_HIGH_STR = "Uh-Oh! That Number is to high! Guess again!"
LOSER_LOW_STR = "Uh-Oh! That number is to low! Guess again!"
DEMAND_STR = "Pick a number between 1 and 10!: "
def do_guess(number, compnumber):
if number > compnumber:
return LOSER_HIGH
elif number < compnumber:
return LOSER_LOW
else:
return WINNER
def main():
compnumber = random.randint (1,10)
print ("Welcome to a guessing game!")
print ("You have 3 try's to guess the correct number!")
for i in xrange(GUESS_TIMES):
number = int(raw_input(DEMAND_STR))
ret = do_guess(number, compnumber)
if ret == LOSER_HIGH:
print LOSER_HIGH_STR
elif ret == LOSER_LOW:
print LOSER_LOW_STR
else:
print WIN_STR
break
else:
print GAMEOVER_STR
raw_input("Please press enter to exit...")
if __name__ == '__main__':
main()