Skip to main content
 首页 » 编程设计

python之输入后如何打印()输出到上一行

2024年12月31日11oomusou

for i in range(1, 10): 
    print('temp {}: '.format(i), end = '') 
    temp = float(input('')) 
    print(' Celsius = {} Fahrenheit'.format(9 / 5 * temp + 32)) 

输出是:

temp 1: 0 
Celsius = 32 Fahrenheit 
temp 2: 5 
Celsius = 41 Fahrenheit 

我想在上一行打印'Celsius =...'。

我想输出为:

temp 1: 0 Celsius = 32 Fahrenheit 
temp 2: 5 Celsius = 41 Fahrenheit 

请您参考如下方法:

我找到了!!!使用 @nik-k在帖子中回答Possible to get user input... :

我在最后一行之前添加了 print('\033[{}C\033[1A'.format(6 + len(str(temp))), end = '')。 '\033[{}(6 表示转到上一行,len(...) 将光标向前移动)C\033[1A'

for i in range(1, 10): 
    print('temp {}: '.format(i), end = '') 
    temp = float(input('')) 
    print('\033[{}C\033[1A'.format(6 + len(str(temp))), end = '') 
    print(' Celsius = {} Fahrenheit'.format(9 / 5 * temp + 32))