如何构建命令行下的Python菜单选项?

Python026

如何构建命令行下的Python菜单选项?,第1张

import time

#from 调用VBS语音播读 import h_读

def h_读(a):

return a

wt1 = input(h_读('您好,欢迎古灵阁,请问您需要帮助吗?1需要or2不需要?'))

if wt1 == '1' or wt1 == '需要':

wt2 = input(h_读('请问您需要什么帮助呢?1存取款,2货币兑换,3咨询'))

if wt2 == '1' or wt2 == '存取款':

print(h_读('小精灵推荐你去存取款窗口;'))

elif wt2 == '2' or wt2 == '货币兑换':

print(h_读('金加隆和人民币的兑换率为1:51.3,即一金加隆=51.3人民币,桥等于10亿'))

time.sleep(1)

wt3 = input(h_读('请问您需要兑换多少金加隆呢?'))

time.sleep(1)

if wt3 == '桥':

print(h_读('恭喜你中奖了。'))

try:

wt33 = float(wt3)

print(h_读('好的,我知道了,您需要兑换' + wt3 + '金加隆。'))

time.sleep(1)

print(h_读('正在计算'))

time.sleep(1)

wt34 = wt33 * 51.3

wt35 = str(wt34)

print(h_读('那么,您需要付给我' + wt35 + '人民币'))

except:

if wt3 != '桥':

print(h_读('你输入的不是数字。把你关起来呀'))

else:

print(h_读('小精灵推荐你去咨询窗口'))

else:

print(h_读('好的,再见。'))

input(h_读("再见"))

这个实例你运行一下就明白了

从用户获取文件名并创建同名文件的函数。然后,函数应该使用while循环来处理文件,并允许用户输入要写入文件的多个整数。

使用for循环读取文件内容并将其输出到屏幕的函数。

向文件中追加若干整数的函数。

计算文件中包含的数字总数并打印

#!/usr/bin/env python3  # py 3.6+

"""

#要求做一个系统菜单,输入数字进入对应菜单,包含以下内容,正常操作不能报错:

# 菜单1:列印所有产品价格和库存

# 菜单2:修改产品价格

# 菜单3:增加库存

# 菜单4:购买指定数量产品

# 菜单5:增加新产品 作为思考题

# 菜单0:退出当前系统

"""

price = {'vegetables': '3','eggs': '4','rice': '2'}  # 价格dict

stock = {'vegetables': '0','eggs': '0','rice': '0'}  # 库存dict

tip = '''

1:列印所有产品价格和库存

2:修改产品价格

3:增加库存

4:购买指定数量产品

5:增加新产品 作为思考题

0:退出当前系统

'''

def main():

    while True:

        global price, stock

        a = input(f'Please enter a number:{tip}\n').strip()

        if a == '0':

            print('Exit!')

            break

        elif a == '1':

            style = '{:15}{:6}{:5}'

            print(style.format('Name', 'price', 'stock'))

            for (n, p), (_, s) in zip(price.items(), stock.items()):

                print(style.format(n, p, s))

            print()

        elif a == '2':

            while True:

                n = input('enter a product name to modify its price: ')

                if n in price:

                    break

                print('invalid input! Should be "{}".'.format(

                    '" or "'.join(price)))

            p = input('enter a new price of this product: ')

            price[n] = p

        elif a == '3':

            while True:

                n = input('enter a product name to increase its stock: ')

                if n in stock:

                    break

                print('Invalid input! Should be "{}".'.format(

                    '" or "'.join(stock)))

            while True:

                s = input('enter a integer to update the stock of it: ')

                try:

                    s = int(s)

                    break

                except:

                    print('Invalid input, must be a integer!')

            stock[n] = str(int(stock[n]) + s)

        elif a == '4':

            while True:

                n = input('enter a product name to buy it: ')

                if n in stock:

                    break

                print('Invalid input! Should be "{}".'.format(

                    '" or "'.join(stock)))

            while True:

                s = input('enter a integer for how many to buy: ')

                try:

                    s = int(s)

                    if s <=0 or s > int(stock[n]):

                        raise

                    break

                except:

                    print('Invalid input, must be a positive integer and '

                        'less than{}!'.format(stock[n]))

            y = input('You want to buy {} X {}, which cost {}? (y)/n '.format(

                n, s, int(price[n]) * s))

            if y.strip().lower() in ('y', ''):

                stock[n] = str(int(stock[n]) - s)

                print('You pay {} and get {} {}'.format(int(price[n]*s), s, n))

        elif a == '5':

            print('Uncomplete...\n')

if __name__ == '__main__':

    main()