怎么做这个循环

2024-05-14 21:38:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我根本不知道如何让这个程序问我是否想重新开始。我知道这很简单,但我不能得到正确的语法为任何我尝试。我想让它在F或C处循环,我会设法解决如何打破。谢谢你的耐心。你知道吗

temp=input('input F or C:  ')
if temp=='F':
    print('convert Fahenheit to C')
    F=int(input("What are degress F:  "))
    c=(F-32)*(5/9)
    print(c)
elif temp=='C':
    print('convert C to F')
    C=int(input("What degrees C would you like to convert to F?  "))
    F=(C*9/5)+32
    print(F)

Tags: orto程序convertinputif语法what
2条回答

下面是一个可能的解决方案(使用Python2.x):

temp = None
while temp != 'Q':
    temp=raw_input('input F or C (Q to quit):  ')
    if temp=='F':
        print('convert Fahenheit to C')
        F=int(raw_input("What are degress F:  "))
        c=(F-32)*(5.0/9.0)
        print(c)
    elif temp=='C':
        print('convert C to F')
        C=int(raw_input("What degrees C would you like to convert to F?  "))
        F=(C*9.0/5.0)+32
        print(F)
while True:
    temp=input("(F)->C, (C)->F, or (Q)uit: ")
    if temp.lower() == "f":
        f = float(input("Enter temperature in Fahrenheit: "))
        c = (f-32)*(5.0/9.0)
        print(c)
    elif temp.lower() == "c":
        c = float(input("Enter temperature in centigrade: "))
        f = (c*9.0/5.0) + 32.0
        print(f)
    elif temp.lower() == "q":
        break

使用5/99/5处于危险地带:在Python2.x中,这是一个整数除法,所以5/9 == 0,而在Python3.x中,这将转换为浮点。你知道吗

(您使用的是input,我认为这意味着您运行的是python3.x,但仍然应该小心确保您知道是整数还是浮点值。)

相关问题 更多 >

    热门问题