I'm new to python and programming in general and I'm trying to make a program that takes as many values and convert that value in a specific choice of temperature scale. But I'm stuck and it keeps giving me a valueerror. If there's any other way making this program work, in for loop for example, i'm happy to learn! Thanks in advance.
here is my code:
mylist = []
temp = int(input("How many temperature values do you want to take? "))
tuple(mylist)
def convert_temp(temp_scale, source_temp):
if temp_scale == "F" or "f":
return 'C', (source_temp - 32) * 5.0 / 9.0
elif temp_scale == "C" or "c":
return 'F', (source_temp * (9.0 / 5.0)) 32
temp_scale = input("Select F or C: ")
source_temp = int(input("What are the temperature? "))
s, m = convert_temp(temp_scale, source_temp)
print(source_temp, 'degrees', temp_scale.title(), 'is', m, 'degrees', s)
output:
How many temperature values do you want to take? 5
Select F or C: F
What are the temperature? 80, 90, 100
Traceback (most recent call last):
File "c:\users\raini\mu_code\blank.py", line 12, in <module>
source_temp = int(input("What are the temperature? "))
ValueError: invalid literal for int() with base 10: '80, 90, 100'
CodePudding user response:
int()
expects to take a string that can be represented as a single integer, not multiple! The simplest way to handle your case in particular is probably with a loop:
temp_scale = input("Select F or C: ")
source_temp = int(input("What are the temperature? "))
# str.split() will convert a string into a list of substrings,
# which were separated in the main string by the given character
# e.g. in this case, "80, 90, 100" -> ["80", " 90", " 100"]
for temp in source_temp.split(','):
# will iterate once for every item in the thing we're iterating over
# in this case, will iterate three times:
# temp = "80", temp = " 90", and temp = " 100"
s, m = convert_temp(temp_scale, temp)
print(temp, 'degrees', temp_scale.title(), 'is', m, 'degrees', s)
In this case, I opted to preserve the structure of your original code, and print a new line for every given temperature. You can experiment with, perhaps, putting the results s
and m
in their own lists, and then condensing the entire list into a single line of output somehow, after the loop is over.
CodePudding user response:
Actually in line no. 12, you are typing 80, 90, 100 and then converting that to an integer. As "," cannot be converted to int it gives a valueerror. There is no problem with the code.
You can try typing in one integer in the input "What are the temperatures? " in the terminal.
Or you can try this code too:
mylist = []
temp = int(input("How many temperature values do you want to take? "))
tuple(mylist)
def convert_temp(temp_scale, source_temp):
if temp_scale == "F" or "f":
return 'C', (source_temp - 32) * 5.0 / 9.0
elif temp_scale == "C" or "c":
return 'F', (source_temp * (9.0 / 5.0)) 32
temp_scale = input("Select F or C: ")
total_temperatures = int(input("How many temeratures do you want to give? "))
for i in range(total_temperatures):
source_temp = int(input("What is the temperature? "))
s, m = convert_temp(temp_scale, source_temp)
print(source_temp, 'degrees', temp_scale.title(), 'is', m, 'degrees', s)
I hope it worked!
CodePudding user response:
You are close just that you missed out on how to store all the numbers, since you can't convert a str of ints to a single int when gathering the values for source_temp_input
.
mylist = []
temp = int(input("How many temperature values do you want to take?"))
def convert_temp(temp_scale, source_temp):
# checking for both upper and lowercase is unnecessary
# instead lowercase the temp_scale and check if its `f`
if temp_scale.lower() == "f":
return "C", (source_temp - 32) * 5.0 / 9.0
elif temp_scale.lower() == "c":
return "F", (source_temp * (9.0 / 5.0)) 32
temp_scale = input("Select F or C:")
source_temp_input = input("What are the temperatures?")
# Use a list comprehension that splits the string by the comma
# then you remove all the whitespace using strip()
# finally you will need to convert them to int
source_temp = [int(temp.strip()) for temp in source_temp_input.split(",")]
# Now your `source_temp` is a list of ints which you loop over and
# convert the temperature
for idx, temp in enumerate(source_temp):
s, m = convert_temp(temp_scale, temp)
# Use f"" string here instead of just printing the values
# m.2f just rounds off the value m to 2 decimal places
print(f"{source_temp[idx]} degrees {temp_scale.title()} is {m.2f} degrees {s}")
Output:
How many temperature values do you want to take?3
Select F or C:F
What are the temperatures?80, 90, 100
80 degrees F is 26.67 degrees C
90 degrees F is 32.22 degrees C
100 degrees F is 37.78 degrees C
How many temperature values do you want to take?3
Select F or C:C
What are the temperatures?80, 90, 100
80 degrees C is 176.00 degrees F
90 degrees C is 194.00 degrees F
100 degrees C is 212.00 degrees F