簡體   English   中英

如何在代碼開頭添加菜單以選擇用戶想要在 Python 中使用的代碼部分?

[英]How can I add a menu at the start of a code to choose which part of code the use wants to use in Python?

我有2個代碼。 我想將它們放在一個代碼中,然后我可以選擇要使用的代碼。 例如,當您運行代碼時:“如果您希望找到鑽孔將產生的體積,請按 1,如果您希望通過給出長度和半徑來找到體積,請按 2:”類似的東西。

import math
pi = math.pi
a = float(input("What is the minimum volume that is required? (cubic meters): "))
b = float(input("How long do you need the tunnel to be? (metres): "))
r2 = a/(pi)/b
r = math.sqrt(r2)
rrounded = (round(r*4)/(4))
if rrounded < r:
    new_radius = (rrounded+0.25)
    print("Bore radius size required: " + str(new_radius))
else:
    new_radius=rrounded
    print(str(new_radius))
total_volume = ((pi)*(new_radius**2)*(b))
total_volume1 = str(round(total_volume, 2))
print("Exact volume produced (with bore-size above and the tunnel length specified): " +
(str(total_volume1)))
import math
pi = math.pi
h = float(input('How long does the tunnel need to be?(meters) '))
r = float(input('What is the radius of the tunnel? '))
volume = pi*r**2*h
volume_ = str(round(volume, 2))
print(volume_)

我為我試圖縮進的可怕嘗試道歉,我還沒有完全弄清楚如何在這里正確地提問。 正如我之前所說,我想使用一個代碼,讓用戶可以選擇在代碼運行時他們想要做什么。 謝謝!

在菜單中定義你想要的功能並做這樣的事情

def menu():
    print('The options are bellow:\n\
                1. ..\n\
                2. ..\n\n')
    menu = input('Choose the option:\n')
    if menu == '1':
        return function1()
    if menu == '2':
        return function2()
    else:
        print('error')
        return menu() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM