在分配之前,它一直在说我的函数。也有人可以告诉我,如果我正确地进行递归 [英] It keeps saying my functions referenced before assignment. Also can someone tell me if I'm putting recursion in correctly

查看:49
本文介绍了在分配之前,它一直在说我的函数。也有人可以告诉我,如果我正确地进行递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import house
def main():
    determine_house()
    determine_phone()
    determine_car()
    def determine_house(repeat):
        house_price=float(input('Enter the price of the house you were going to pay'))
        num_beds=int(input('Enter the number of bedrooms'))
        house=house.house(house_price,num_beds)
        foo=house.get_average_price()
        woo=house.get_bedrooms()
        if foo==218000:
            if woo==2:
                return ' You are paying average price for your house '
            elif woo==1:
                return ' You are being overpriced for this house relative to bedrooms '
                determine_house(repeat-1)
            else:
                return ' you are getting a good deal for your house '
        elif foo>218000:
            return ' You are paying too much for a house in general '
        else:
            return ' You are getting a good price for this house in genereal  '
    def determine_phone(repeat):
        phone_price=float(input(' Enter the price of the phone you were going to pay '))
        type_phone=input(' Enter the type of phone ')
        phone=phone.house(phone_price,type_phone)
        voo=phone.get_average_price()
        coo=phone.get_type_of_phone()
        if coo==' Android ':
            if voo==254:
                return ' you are paying average price '
            elif voo>254:
                return ' you are being overpriced '
                determine_phone(repeat-1)
            else:
                return ' you are getting a good deal for this phone '
        elif coo==' iphone ':
            if voo==687:
                return ' you are paying average price for this phone '
            elif voo>687:
                return ' you are being overpiced for this phone '
                determine_phone(repeat-1)
            else:
                return ' you are getting a good deal for this phone '
        else:
            print('this type of phone is not supported by the engine')
    def determine_car(repeat):
        car_price=float(input(' Enter the price of the car you were going to pay '))
        model=input('Enter the model of the car youre thinking of buying')
        car=car.house(car_price,model)
        boo=car.get_average_price()
        loo=car.get_model()
        if loo==' toyota camry ':
            if boo==23947:
                return ' you are paying average price for this car '
            elif boo>23947:
                return 'you are being overpriced for this car'
                determine_car(repeat-1)
            else:
                return 'you are getting a good deal for this car'
        elif loo==' Honda Accord ':
            if boo==23720:
                return ' you are paying average price for this car '
            elif boo>23720:
                return ' you are being overpiced for this car '
                determine_car(repeat-1)
            else:
                return ' you are not getting a good deal for this house '
        else:
            return ' this model of car is not supported by the engine '

main()





我尝试了什么:



我试过把函数调用放到底部顶部





What I have tried:

ive tried putting the function calls at the bottom and top

line 4, in main
    determine_house()
UnboundLocalError: local variable 'determine_house' referenced before assignment

推荐答案

引用:

它保持在分配之前说我的函数被引用。

It keeps saying my functions referenced before assignment.



在Python中,缩进是程序的结构。定义一个没有缩进的函数开始。

这应该更好:


In Python, indentation is the structure of your program. defining a function start with no indentation.
This should be better:

import house
def main():
    determine_house()
    determine_phone()
    determine_car()
def determine_house(repeat):
    house_price=float(input('Enter the price of the house you were going to pay'))
    num_beds=int(input('Enter the number of bedrooms'))
    house=house.house(house_price,num_beds)
    foo=house.get_average_price()
    woo=house.get_bedrooms()
    if foo==218000:
        if woo==2:
            return ' You are paying average price for your house '
        elif woo==1:
            return ' You are being overpriced for this house relative to bedrooms '
            determine_house(repeat-1)
        else:
            return ' you are getting a good deal for your house '
    elif foo>218000:
        return ' You are paying too much for a house in general '
    else:
        return ' You are getting a good price for this house in genereal  '
def determine_phone(repeat):
    phone_price=float(input(' Enter the price of the phone you were going to pay '))
    type_phone=input(' Enter the type of phone ')
    phone=phone.house(phone_price,type_phone)
    voo=phone.get_average_price()
    coo=phone.get_type_of_phone()
    if coo==' Android ':
        if voo==254:
            return ' you are paying average price '
        elif voo>254:
            return ' you are being overpriced '
            determine_phone(repeat-1)
        else:
            return ' you are getting a good deal for this phone '
    elif coo==' iphone ':
        if voo==687:
            return ' you are paying average price for this phone '
        elif voo>687:
            return ' you are being overpiced for this phone '
            determine_phone(repeat-1)
        else:
            return ' you are getting a good deal for this phone '
    else:
        print('this type of phone is not supported by the engine')
def determine_car(repeat):
    car_price=float(input(' Enter the price of the car you were going to pay '))
    model=input('Enter the model of the car youre thinking of buying')
    car=car.house(car_price,model)
    boo=car.get_average_price()
    loo=car.get_model()
    if loo==' toyota camry ':
        if boo==23947:
            return ' you are paying average price for this car '
        elif boo>23947:
            return 'you are being overpriced for this car'
            determine_car(repeat-1)
        else:
            return 'you are getting a good deal for this car'
    elif loo==' Honda Accord ':
        if boo==23720:
            return ' you are paying average price for this car '
        elif boo>23720:
            return ' you are being overpiced for this car '
            determine_car(repeat-1)
        else:
            return ' you are not getting a good deal for this house '
    else:
        return ' this model of car is not supported by the engine '

main()




引用:

现在告诉我



determine_house()

TypeError:determine_house()缺少1个必需的位置参数:'repeat'并且它说它期望一个缩进的块,其中每个函数都被定义

now its telling me

determine_house()
TypeError: determine_house() missing 1 required positional argument: 'repeat' and its saying its expecting an indented block where each function is defined




import house
def main():
    determine_house() # it says there is a parameter missing between parenthesis
    determine_phone()
    determine_car()
def determine_house(repeat): # because here you say a parameter is requested



所以解决方案是:


So a solution is:

import house
def main():
    determine_house(1) # it says there is a parameter missing between parenthesis
    determine_phone()
    determine_car()
def determine_house(repeat): # because here you say a parameter is requested



但是同样的消息会因为完全相同的原因而为Determ_phone()弹出,一旦你解决了它,你就会在determine_car()上有相同的信息。



但问题是,在你的情况下,递归并不是你需要的。

看起来你几乎不了解语言是如何工作的,大量的重写是为了你的代码。

建议:按照一些教程熟悉语言的常用方面,如函数参数和返回行为。


But the same message will pop for determine_phone() for exact same reason, and once you solved it, you will have the same on determine_car().

But the whole problem is that in your case, recursion is not what you need.
looks like you barely understand how the language works and large rewrite is in order for your code.
Advice: follow some tutorial to get familiar with usual aspects of the language like parameters of functions and behavior of return.


这篇关于在分配之前,它一直在说我的函数。也有人可以告诉我,如果我正确地进行递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆