Python程序仅在我按Enter键时出现

时间:2013-12-12 10:31:33

标签: python

当我运行已经打开IDLE的程序时,有时需要我按 Enter 才能显示文本。

如何让它消失?

代码:

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()

感谢。

1 个答案:

答案 0 :(得分:0)

您必须始终尝试在函数本身中包含函数所需的任何用户输入变量!此外,您忘记键入dicesidesint,因为input()会返回一个字符串。另外,IMO,函数参数都没用,你可以在函数本身中询问它们。

我会按以下方式进行。

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,我不认识你。使用上面的代码,无论如何都不会发生。