Python函数在调用第二个tim时跳过一个段

2024-09-22 20:18:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我能写的最少的代码,它复制了我在更大的代码段中遇到的问题,它是针对python3.4.2的

主要的问题是我无法正确调用我的函数。 我可以很好地浏览一下这个程序,但是当我再次调用“PrisonCellSize”函数重新启动程序时,输入部分被完全跳过了,有人知道如何解决这个问题吗?我想,因为我重置roomheight和roomsize为“0”,它将需要用户再次输入?相反,它完全跳过了输入部分,它可能与我的意图有关吗?你知道吗

我对这一点非常陌生,所以如果我犯了一个明显的错误,我很抱歉,但在放弃之前,我已经尝试了很长时间的故障排除,并询问各位天才:p我将非常感谢任何帮助解决这个问题Python版本:3.4.2

第二次调用“PrisonCellSize”函数时,不会要求用户输入

编辑:格式化,我希望这对每个人都更清楚

def PrisonCellSize():
    global TotalRoomSize    
    #calculates size of the room
roomheight = 0
roomwidth = 0

roomwidth = float(input("Enter your total room width, make sure it is between 5 and 10 metres:\n"))
roomheight = float(input("Enter the room height, it must be between 1m-4m: \n")) 
if ((roomwidth <=10 and roomwidth >=5) and (roomheight <=4 and roomheight >=1)):
    print ("Thankyou for your input\n")

else: print ("\nError, please enter the correct amounts")

def NextFunction():
    input ("Did you want a window:\n")

def restart():
    print ("restarting program\n")
    print ("--------------------")
    PrisonCellSize()
    NextFunction()
    restart()

    return  




#start of program

PrisonCellSize()
NextFunction()
restart()

我的结果如下:

"================================ RESTART ================================ 
Enter your total room width, make sure it is between 5 and 10 metres:
"8"
Enter the room height, it must be between 1m-4m: 
"2"
Thankyou for your input

Did you want a window:
"yes"
restarting program

--------------------
Did you want a window:
"yes"
restarting program

--------------------
Did you want a window:
"yes"
restarting program

--------------------
Did you want a window:"       

Tags: andtheyouinputyouritwindowprogram
1条回答
网友
1楼 · 发布于 2024-09-22 20:18:38
def PrisonCellSize():
    global TotalRoomSize    
    #calculates size of the room
    roomheight = 0
    roomwidth = 0

    roomwidth = float(input("Enter your total room width, make sure it is between 5 and 10 metres:\n"))
    roomheight = float(input("Enter the room height, it must be between 1m-4m: \n")) 
    if ((roomwidth <=10 and roomwidth >=5) and (roomheight <=4 and roomheight >=1)):
        print ("Thankyou for your input\n")

    else: print ("\nError, please enter the correct amounts")

def NextFunction():
    input ("Did you want a window:\n")

def restart():
    print ("restarting program\n")
    print ("          ")
    PrisonCellSize()
    NextFunction()
    restart()

    return  

相关问题 更多 >