'str'对象不能解释为整数(Python 3.8)

时间:2020-05-15 11:26:56

标签: python string

我知道之前曾多次问过这个问题,但是我无法弄清楚我的代码有什么问题以及如何解决它。

这是一个温度转换器

https://1515143793.cloud.vimeo.com/upload?ticket_id=323834021&video_file_id=1807184661&signature=some_number&v6=1&redirect_url=https%3A%2F%2Fvimeo.com%2Fupload%2Fapi%3Fvideo_file_id%some_number%26app_id%some_number%26ticket_id%some_number%26signature%some_number

并显示错误:

import sys
import time
print('This program will convert different units of temperature.')
unit1 = input('What unit do you want to convert from?\nAnswer with the letter C, F or K.\n')
unit2 = input('And what unit to?\nUse the same ruling as the previous question.\n')
temp1 = input('What temperature do you want to convert?.\n')
temp2 = 0

if unit1 == ('C'):
    symbo1one = ('°C')

if unit1 == ('K'):
    symbolone = ('°K')

if unit1 == ('F'):
    symbolone = ('°F')


if unit2 == ('C'):
    symbo12 = ('°C')

if unit2 == ('K'):
    symbol2 = ('°K')

if unit2 == ('F'):
    symbol2 = ('°F')



if unit1 == ('C') and unit2 == ('K'):
    temp2 = ((int(temp1)) + 273.15)
    unitname1 = ('Celsius')
    unitname2 = ('Kelvin')
if unit1 == ('C') and unit2 == ('F'):
    temp2 = ((int(temp1) * (9/5)) + 32)
    unitname1 = ('Celsius')
    unitname2 = ('Farhenheit')
if unit1 == ('K') and unit2 == ('C'):
    temp2 = ((int(temp1)) - 273.15)
    unitname1 = ('Kelvin')
    unitname2 = ('Celsius')
if unit1 == ('K') and unit2 == ('F'):
    temp2 = ((((int(temp1)) - 273.15) * (9/5)) + 32)
    unitname1 = ('Kelvin')
    unitname2 = ('Farhenheit')
if unit1 == ('F') and unit2 == ('C'):
    temp2 = (((int(temp1)) + 32) * (5/9))
    unitname1 = ('Farhenheit')
    unitname2 = ('Celsius')
if unit1 == ('F') and unit2 == ('K'):
    temp2 = ((((int(temp1)) + 32) * (5/9)) + 273.15)
    unitname1 = ('Farhenheit')
    unitname2 = ('Kelvin')
if unit1 == unit2:
    temp2 = temp1

dp = ('To how many decimal places would you like your result given?')

print(('Original Temperature: ') + (str(temp1)) + (symbolone))
time.sleep(1)
print(('Converted Temperature: ') + (round(temp2, (dp))) + (symbol2))
time.sleep(10)
sys.exit()

我已经尽力了,并且我对编程还比较陌生,所以非常感谢您的帮助

2 个答案:

答案 0 :(得分:1)

两件事。在最后两行中,您忘记将input添加到dp中,并且无法将round值添加到字符串中,因此需要转换为字符串。

这将起作用

dp = int(input('To how many decimal places would you like your result given?'))
print(('Original Temperature: ') + (str(temp1)) + (symbolone))
time.sleep(1)
print(('Converted Temperature: ') + (str(round(temp2, (dp)))) + (symbol2))

答案 1 :(得分:0)

dp = ('To how many decimal places would you like your result given?')

您是要用它来从用户那里得到一些东西吗?因为,如果这样做,它应该类似于:

dp = int(input('To how many decimal places would you like your result given?'))