如何在spyder中执行简单的交互式程序?

时间:2017-05-01 15:20:54

标签: python-3.x ide spyder interactive

我写了一个典型的猜数游戏:

import random
secret = random.randint(1, 99) 
guess = 0
tries = 0
print("Hey you on board! I am the dreadfull pirat Robert, and I have a 
secret!")
print("that is a magic number from 1 to 99. I give you 6 tries.")
while guess != secret & tries < 6:
    guess = input()
    if guess < secret:
        print("You! Son of a Biscuit Eater! It is too little! YOU Scurvy dog!")
    elif guess > secret:
        print("Yo-ho-ho! It is generous, right? BUT it is still wrong! The 
number is too large, Savvy? Shiver me timbers!")   
    tires = tries + 1
if guess == secret:
    print("Enough! You guessed it! Now you know my secret and I can have a peaceful life. Take my ship, and be new captain")
else:
    print("You are not lucky enough to live! You do not have ties. But before you walk the plank...")
    print("The number was ", secret)
    print("Sorry pal! This number became actually you death punishment. Dead men tell no tales! Yo Ho Ho!")

然而,Spyder执行它并没有停止用户输入数字而我只得到了这个输出:

  

嘿,你在船上!我是一个可怕的海盗罗伯特,我有一个秘密!   这是一个从1到99的幻数。我给你6次尝试。   你生活得不够幸运!你没有联系。但在你走板之前......   这个数字是56   对不起朋友!这个数字实际上成了你的死刑。死人不会告密! Yo Ho Ho!

我试图调用cmd - &gt; spyder并在那里执行(通过复制粘贴),但我遇到了很多错误,如:

  

打印(“号码是”,秘密)     文件“”,第1行       打印(“数字是”,秘密)

但是,执行此代码行(至少所有带有打印的行)行不是问题。

如何以交互方式执行我的代码,以便用户可以给出一个数字然后继续游戏?

1 个答案:

答案 0 :(得分:1)

Couple of issues with your code, in the tires=tries+1 you've probably made a code typo.

Second, guess reads in a string so you will need to convert guess into an int to do integer comparisons, use something like guess=int(guess).

The reason you aren't seeing this is because your condition in the while loop does not execute as true, run guess != secret & tries < 6 in the interpreter and you'll see that the condition is false.

Instead you should use and as this is a logical operator the & is a bitwise logical operator (they are not the same).

while guess != secret and tries < 6: is the appropriate line of code you should substitute.