python - 需要帮助制作 "python"程序 - 当我给出输入(1 或 2)时,如何让我的选择菜单启动不同的转换

标签 python

我一直在研究这个温度转换器,但在结合这两个程序(摄氏度到华氏度)和(华氏度到摄氏度)时遇到了麻烦,我可以打印显示菜单,但无法弄清楚如何选择转换器。

# Tempeture Converter
def convert():
    print 'Conversions Menu';
    print '(1) Celsius to Fahrenheit';
    print '(2) Fahrenheit to Celsius';

def select():
    convert();
    choice = input ('Enter Choice Number:')
    if (input == '1'):
        C2F();
    elif (input == '2'):
        F2C();

def F2C():
    Fahrenheit = input('enter degrees in Fahrenheit: ');
    Celsius    = ( 5.0 / 9.0) * (Fahrenheit -32); 
    print Fahrenheit, 'Fahrenheit =', Celsius, 'Celsius';

def C2F():
    Celsius    = input('enter degrees in Celsius: ');
    Fahrenheit = (9.0 / 5.0) * Celsius +32;
    print Celsius, 'Celsius =', Fahrenheit, 'Fahrenheit';

*更正*

# Tempeture Converter
def convert():
    print 'Conversions Menu';
    print '(1) Celsius to Fahrenheit';
    print '(2) Fahrenheit to Celsius';

def select():
    convert();
    choice = input ('Enter Choice Number:')
    if (choice == '1'):
        C2F();
    elif (choice == '2'):
        F2C();

def F2C():
    Fahrenheit = input('enter degrees in Fahrenheit: ');
    Celsius    = ( 5.0 / 9.0) * (Fahrenheit -32); 
    print Fahrenheit, 'Fahrenheit =', Celsius, 'Celsius';

def C2F():
    Celsius    = input('enter degrees in Celsius: ');
    Fahrenheit = (9.0 / 5.0) * Celsius +32;
    print Celsius, 'Celsius =', Fahrenheit, 'Fahrenheit';

最佳答案

select函数中,你有

choice = input('...')

然后你过牌

if (input == '1'):
    ...

第二个 input 应该是 choice。同样的事情也适用于 elif(input == '2'): 语句。同样,input 应替换为 choice。或者,您可以按照 J.F. 的建议将 raw_input'1''2' 一起使用。

此外,input 函数会自动将 12 转换为整数,因此您真的应该检查 if (choice == 1 )2 类似。

最后,您需要一些实际运行 select() 函数的表单,例如

if __name__ == '__main__':
    select()

关于python - 需要帮助制作 "python"程序 - 当我给出输入(1 或 2)时,如何让我的选择菜单启动不同的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7813916/

相关文章:

python - 无法使用 Django Rest 框架发送压缩的 gzip 数据

python - 关闭文件的 I/O 操作 : Django Imagekit & Pillow

python - Django 2.0 slug urls.py 意外关键字参数 'slug'

python - 如何使用 python 3 对文件执行上下文菜单操作

tk 中的 Python 可滚动小部件

python - Python 中的选择和管道问题

python - 使用numpy使用6个线性方程求解5个变量

python - 有没有办法让 python 在没有外部模块的情况下检测音频加载器而不是某个级别的 db

python - 全卷积网络——训练数据中不同大小的图像

python - 在 Heroku 上托管的 Flask 应用程序中渲染 Bokeh 图