如何使另一个函数的变量在另一个Python上运行?

时间:2017-09-08 02:06:28

标签: python

所以这是我的整个代码,当我在window_Calculate()中没有定义高度时,我遇到了麻烦。我的目标是使用公式50 + (height * width) * 100。在window_Size中,用户必须输入heightwidth,但如果他们输入的不是数字,则应该返回它以便输入实际数字。总的来说,height在另一个函数中没有定义,如果用户输入的数字不是数字,则不返回输入。如何定义height并返回输入?

import time 

def Intro():
    print("Welcome to Wendy's Windows.")
    time.sleep(2)
    print("This is a window replacement business.")
    time.sleep(2)
    print("Enter your size in the box on the left and the price will be displayed below.")
    time.sleep(2.5)

def window_Size():
    print("Put height first then put width.")
    time.sleep(1.5)
    height = input("Height: ")
    try:
        int(height)
    except ValueError:
         try:
            float(height)
         except ValueError:
            print("Enter only numbers.")
            return(height)
    else:
        print(height + "m")
    width = input("Width: ")
    try:
        int(width)
    except ValueError:
        try:
            float(width)
        except ValueError:
                print("Enter only numbers.")
                return(width)
    else:
        print(width + "m")

def window_Calculate():
    window_Calculate = '50' + (height * width) * '100'

def window_Cost():
    print("Your price is" + window_Calculate + "$")

window_Size()
window_Calculate()
window_Cost()

3 个答案:

答案 0 :(得分:2)

所以这里有几件事情:

  1. window_Size()的{​​{1}}类型测试
  2. try/except调用window_Size()但未存储
  3. return(height)使用非局部变量但没有给出参数
  4. 尝试做的是使用所谓的window_Calculate()。几乎任何编程语言都不鼓励使用它们。相反,您应该使用在global variable变量返回到更高调用函数的被调用函数范围内的local variable

    下面我已经指出了你的程序中的一些亮点,以及你的程序的更改。虽然是一个解决方案,但我不熟悉您对这些概念的了解程度,因此只需稍微修改一下您的程序即可明确说明这一点。然而,这可以被显着改变为更多 pythonic 和精炼。

    数字1:

    return

    所以你使用的def window_Size(): print("Put height first then put width.") time.sleep(1.5) # bit annoying to sleep all the time? height = input("Height: ") try: int(height) # type test of int, that's fine except ValueError: # correct catch try: float(height) # type test of float, that's fine except ValueError: # correct catch; must be invalid print("Enter only numbers.") # message for user return(height) # wait why did you return?? else: # wont be executed if above return() is called (i.e. for str()) print(height + "m") width = input("Width: ") try: int(width) except ValueError: try: float(width) except ValueError: print("Enter only numbers.") return(width) # All the same notes above apply else: print(width + "m") 非常好,但你在一个函数中有2个try/except/else个调用;一旦被调用,return()将退出(即如果window_Size()被调用则没有width。此外,要返回多个值,您可以使用return(height)。< / p>

    数字2:

    tuple

    数字3:

    window_Size() # this functions returns values, but isnt storing
    

    <强>解决方案:

    window_Calculate() # this functions uses returns values, but isnt passed any
    

答案 1 :(得分:-1)

你应该确保height,width和window_Calculate是全局变量,所以你可以在A函数中更改它们,并在B函数中使用它们。

答案 2 :(得分:-1)

pstatix的答案是最好的解决方案,应该被接受

你需要拥有&#34;全球&#34;变量每个方法都可以获取或设置。试试这个:

import time 

window_height = 0
window_width = 0
window_value = 0

def Intro():
    print("Welcome to Wendy's Windows.")
    time.sleep(2)
    print("This is a window replacement business.")
    time.sleep(2)
    print("Enter your size in the box on the left and the price will be displayed below.")
    time.sleep(2.5)

def window_Size():
    global window_height
    global window_width
    print("Put height first then put width.")
    time.sleep(1.5)
    height = input("Height: ")
    try:
        int(height)
    except ValueError:
         try:
            float(height)
         except ValueError:
            print("Enter only numbers.")
            return(height)
    else:
        print(str(height) + "m")
        window_height = height
    width = input("Width: ")
    try:
        int(width)
    except ValueError:
        try:
            float(width)
        except ValueError:
                print("Enter only numbers.")
                return(width)
    else:
        print(str(width) + "m")
        window_width = width

def window_Calculate():
    global window_value
    window_value = 50 + window_height * window_width * 100

def window_Cost():
    print("Your price is $" + str(window_value))











window_Size()
window_Calculate()
window_Cost()