gpt4 book ai didi

Python 程序仅在我按下回车键时出现

转载 作者:行者123 更新时间:2023-12-05 07:59:50 26 4
gpt4 key购买 nike

当我在 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()

谢谢。

最佳答案

您必须始终尝试在函数本身中包含您的函数需要的任何用户输入变量!此外,您忘记了将 dicesides 类型转换为 int,因为 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,我不明白你的意思。使用上面的代码,无论如何都不会发生这种情况。

关于Python 程序仅在我按下回车键时出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20541001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com