2013-12-12 33 views
2

当我运行程序时IDLE已经打开,它有时需要我按输入才能显示文本。只有当我按下回车键时才会出现Python程序

我该如何让它消失?

代码:

import random 

def var(): 

    dice_score = 0 
    repeat = "" 
    dicesides = input("Please enter the amount of sides you want the dice to have.\n The amounts you can have are as follows: 4, 6 or 12: ") 
    script(dice_score, dicesides, repeat) 

def script(dicescore, dicesides, repeat): 

    if dicesides in [4,6,12]: 
     dice_score = random.randrange(1, dicesides) 
     print(dicesides, " sided dice, score ", dice_score, "\n") 
    else: 
     print("Please Try Again. \n") 
     var() 
    repeat = str(input("Repeat? Simply put yes or no: ").lower()) 

    if repeat == "yes": 
     var() 
    else: 
     quit() 

var() 

感谢。

+0

如果您需要回答或其他问题,请附上相关信息。 – 2013-12-12 10:32:38

+0

没有任何信息,当我运行我的python脚本时,我需要按回车才能显示它。 – user3092741

+0

你正在运行的代码是什么?好像你在等待程序运行时的某种额外输入。 –

回答

0

您必须始终尝试在函数中包含您的函数需要的任何用户输入变量!另外,由于input()返回字符串,因此忘记将dicesides打到int。此外,国际海事组织,功能参数是相当无用的,你可以问他们在功能本身。

我会用下面的方法做。

from random import randrange 

def script(): 

    dicesides = int(input("Please enter the amount of sides you want the dice to have.\n The amounts you can have are as follows: 4, 6 or 12: ")) 

    if dicesides in [4,6,12]: 
     dice_score = randrange(1, dicesides) 
     print(dicesides, " sided dice, score ", dice_score, "\n") 
     return True 
    else: 
     print("Please Try Again. \n") 
     return False 

repeat = "yes" 
yes = ["yes", "y", "YES", "Y"] 

while repeat in yes: 
    if not script(): 
     continue 
    repeat = input("Repeat? Simply put yes or no: ").lower() 

至于主要问题needing an extra enter,我不明白你。通过上面的代码,这不会发生。

相关问题