2017-10-12 99 views
0

编辑:在main()了一下周围玩耍后,它似乎是在程序选择哪个函数被调用的,如果/ elif的块不管输入的。我不知道为什么它开始工作的罚款程序不会根据输入调用正确的功能。不知道我缺少

我已经看了后在我的代码这样做,我也弄不清是什么,我没有看到。 这是一个数字序列游戏,它要求用户选择简单和困难之间的困难。它在某一点上工作得很好,但现在无论选择什么,每次都会进入简单模式。即使你没有任何输入就直接回车。

#main program function and difficulty selection 
def main(): 
print('-----------------------------------------------') 
print('Please choose a difficulty.') 
difficulty = str(input('(e)asy|(h)ard: ')) 
print('-----------------------------------------------') 
if difficulty == 'easy'or'e': 
    easy() 
elif difficulty == 'hard'or'h': 
    hard() 

然后我有一个简单的功能,一个很难。 硬只是简单的功能,只改变生成的序列的大小,没有别的。我已经浏览了每个模块,没有任何更改会影响哪个函数被调用。

不管它发生了多少次的游戏的玩法,所以它必须是坏了我的main()函数

的代码的其余部分是在这里有没有什么帮助,也许我失去了一些东西明显。

import random 
def easy(): 
    print ('Easy Mode','\n') 
    #Generates inital number, step value, and upper limit 
    num_sequence = 5 
    numbers = random.randint(1,101) 
    step = random.randint(1,20) 
    #Accumulates and prints all but last number in sequence 
    for num_generated in range (1, num_sequence): 
     print(numbers) 
     numbers = numbers + step 
    #Sets tries allowed and subtracts wrong attempts 
    guesses = 3 
    while guesses > 0: 
     user_num = int(input('Next Number: ')) 
     guesses = guesses - 1 
     if user_num != numbers: 
      if guesses == 0: 
       break 
      else: 
       print('Try Again (Attempts Remaining:', guesses,')')    
     if user_num == numbers: 
      break 

#Prints appropriate message based on game results      
    if user_num == numbers: 
     print ('Correct','\n') 
    if user_num != numbers: 
     print ('Attempts Exceeded: The answer was',numbers,'\n') 

#block for hard difficulty (same as above, sequence size changed to 4) 
def hard(): 
    print ('Hard Mode','\n') 
    num_sequence = 4 

#main program function and difficulty selection 
def main(): 
    print('-----------------------------------------------') 
    print('Please choose a difficulty.') 
    difficulty = str(input('(e)asy|(h)ard: ')) 
    print('-----------------------------------------------') 
    if difficulty == 'easy'or'e': 
     easy() 
    elif difficulty == 'hard'or'h': 
     hard() 

#block for replay selection 
replay = 'y' 
while replay == 'y': 
    main() 
    replay = input('Play again? (y)|(n): ',) 
    print ('\n') 
    if replay == 'n': 
     print('-----------------------------------------------') 
     print('Goodbye!') 
     print('-----------------------------------------------') 
     break 

硬()是相同的代码的那些前几个

回答

0

当你制造复合比较(使用或)的条件的双方必须是完整的后线容易()行。换句话说,

if difficulty == 'easy'or difficulty == 'e': 
    easy() 
elif difficulty == 'hard'or difficulty == 'h': 
    hard() 

否则你说:“如果难度==‘容易’>>这是假的,然后分配给困难‘E’”,这是不是意图。

+0

就是这样。非常感谢。这就是我想要成为幻想的原因。 – Kalivel

+0

以及GET FANCY !!因为这就是我们学习的方式!很好的程序btw! – kir10s

+0

谢谢。记住我是否问过我做了什么?我们只有6个星期的课,所以我在编程时还是很新的。 – Kalivel

相关问题