gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:34 25 4
gpt4 key购买 nike

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

# 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com