Python Forum
Table of Temperatures... Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Table of Temperatures... Help
#1
I'm trying to create a table that converts Celsius to Fahrenheit, I have to use the main() function and the counter-controlled loop "for". When I put in 21, the program will run all the way until 30. How can I get it to stop at the number the user puts in? I need it to range from 0 to the input number.


# Table of Temperatures

start = 1
end = 31
increment = 1

input("Enter a number: ")
print()

def main():
    print("Celcius \t Farenheit")

    def fahrenheit(T_in_celsius):
        return (T_in_celsius * 9 / 5) + 32

    for t in range (start, end, increment):
        print(t, "\t\t ", fahrenheit(t))


main()
Reply
#2
you need to take end as input from the user, not hardcode it. Also as it is in your code, you don't store the user input in a variable
Reply
#3
So I need to store the users input as a variable "end = input("Enter a Number: ")) and store it in the "def t in range (start, end, increments)"?
Reply
#4
more or less, yes.
However,
1. input will return string (str type), and to do calculations  you need integer (int type). So you need to convert the user input from str to int before use in calculations. As you don't need to keep the str, you can do this once, immediately (or even on the same line) with the input.
For your benefit, take time to research how you can do 1, before you ask here on the forum :-) I'm certain your teacher/lector has taught you that already.
2.As it is now, it will be accessible within the main(), i.e. you can use it. However you will run into problems if you try to change it in the main and is a bad programming because you run the risk to get confused with the scope of the name - if you have variable with the same name in the function. That may sound not relevant to your current task, but if it turns to bad habit or misunderstanding of concept - it will byte you in the future.
so, why not take the user input, as well initialize the other names (start, increment) within the main(), instead on module level?.
3. finally, I would take the definition of fahrenheit function out of the main()
Reply
#5
How does this look? 
user = int(input("Enter a Number: "))

def fahrenheit(celcius):
        return (celcius * 9 / 5) + 32
    
def main():
    print("Celcius \t Farenheit")
    
    for t in range (user):
        print(t, "\t\t ", fahrenheit(t))
        
main()

When I enter 25 it will stop at 24 >_<, what did I do wrong this time?
Reply
#6
it does not address second bullet in my previous post :-)
and user is not clear as name, when you actually want user input for max temp in Celsius

why not

def fahrenheit(celsius):
    return (celsius * 9 / 5) + 32
    
def main():
    max_temp_c = int(input("Enter a Number: "))
    print("Celsius \t Fahrenheit")
    
    for t in range(max_temp_c):
        print('{}\t\t{}'.format(t, fahrenheit(t)))
       
main()
Reply
#7
(May-07-2017, 07:28 AM)zepel Wrote: When I enter 25 it will stop at 24 >_<, what did I do wrong this time?
range(n) will return iterable that starts at 1 and stops at n-1, i.e. range(n) does not incl. n
Reply
#8
Thanks for all your help, I'm still a tad bit confused on how to make it finish at the number I entered. I moved the input into the main function.

My teacher has his TA teach the class and we only meet for one hour once a week. I'm very sorry for the stupid questions, this is an intro to programming class.
Reply
#9
(May-07-2017, 07:56 AM)zepel Wrote: I'm still a tad bit confused on how to make it finish at the number I entered

if you want to finish at n (user input) use range(n+1) instead of range(n)
Reply
#10
So in my program, "for t in range(n + 1 (user)):"

def fahrenheit(celcius):
        return (celcius * 9 / 5) + 32
     
def main():

    user = int(input("Enter a Number: "))
    print("Celcius \t Farenheit")
     
    for t in range (user):
        print(t, "\t\t ", fahrenheit(t))
         
main()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020