Python код не работает, ввод (возможно, связанный с меню) - PullRequest
0 голосов
/ 08 июля 2020
import circle
pi = 3.1415

def main():

        area(radius)
        circumference(radius)

def menu():
        
        print("Type a for area of circle")
        print("Type b for circumference of a circle")
        print("Type c to END PROGRAM")     
loop=True

while loop:
        menu()
        choice = input('Please enter your choice: ')

        if choice== "a":
                radius = float(input ("Input the radius of the circle : "))
                print(circle.area(radius))
        elif choice== "b":
                radius = float(input ("Input the radius of the circle : "))
                print(circle.circumference(radius))
        else:
                print("Goodbye!")
                

def area(radius):
    return pi * radius**2


def circumference(radius):
    return 2 * pi * radius

main()

В моем последнем вопросе я получил справку по своему меню (которое сейчас работает!) Однако, когда я ввожу радиус, я получаю ошибку:

AttributeError: частично инициализировано модуль 'circle' не имеет атрибута 'area' (скорее всего, из-за циклического импорта)

Ответы [ 2 ]

0 голосов
/ 09 июля 2020

Я внес некоторые изменения в ваш код, и он должен работать, как задумано. Дайте мне знать, если у вас возникнут вопросы!

По сути, удалите импорт, удалите функцию main () и переместите область () и окружность () вверх.

pi = 3.1415

# function to print the menu options
def menu():
    print("Type a for area of   ")
    print("Type b for circumference of a circle")
    print("Type c to END PROGRAM")

# function to calculate area
def area(radius):
    return pi * radius**2

# function to calculate circumference
def circumference(radius):
    return 2 * pi * radius

# menu loop
while True:

    # display menu
    menu()

    # prompt for user's choice
    choice = input('Please enter your choice: ')

    if choice == "a":
        radius = float(input("Input the radius of the circle : "))
        print(area(radius))
    elif choice == "b":
        radius = float(input("Input the radius of the circle : "))
        print(circumference(radius))
    else:
        print("Goodbye!")
        break
0 голосов
/ 08 июля 2020

Убить import circle строку. Измените circle.area на area и circle.circumference на circumference. Переместите определения функций area и circumference наверх, чтобы они были определены перед использованием. Убейте строку main() в конце. Прочтите https://docs.python.org/3/tutorial/index.html вместо того, чтобы набирать маги c заклинания, которые вы не понимаете, и надеетесь, что они как-то сработают :)

...