2013-12-12 35 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,我不明白你。通過上面的代碼,這不會發生。

相關問題