Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to complete my code assignment and cannot figure out how to display results for (months, days, hours and seconds) all on one function display. Below are my teacher comments. Any help is appreciated!
P.S I am new to coding.

Activity 2: Output is incorrect. All time selections are displaying "seconds" for the label. You can only have one display_results function. See the example code for how to have one function display multiple labels depending on call.

What I have tried:

display_results(months, days, hours, seconds):
print(str(months) + str(days) + str(hours) + str(seconds))

Python
# This program asks the user to select Fahrenheit or Celsius conversion
# and input a given temperature. Then the program converts the given 
# temperature and displays the result.
#
# References:
#     https://www.mathsisfun.com/temperature-conversion.html
#     https://en.wikibooks.org/wiki/Python_Programming


def get_choice():
    print("Enter C to convert to Celsius or F to convert to Fahrenheit:")
    choice = input()    
    return choice


def process_celsius():
    temperature = get_temperature("Fahrenheit")
    result = calculate_celsius(temperature)
    display_result (temperature, "Fahrenheit", result, "Celsius")


def process_fahrenheit():
    temperature = get_temperature("Celsius")
    result = calculate_fahrenheit(temperature)
    display_result (temperature, "Celsius", result, "Fahrenheit")


def get_temperature(scale):
    print("Enter " + scale + " temperature:")
    temperature = float(input())    
    return temperature


def calculate_celsius(fahrenheit):
    celsius = (fahrenheit - 32) * 5 / 9    
    return celsius


def calculate_fahrenheit(celsius):
    fahrenheit = celsius * 9 / 5 + 32
    return fahrenheit


def display_result(temperature, fromScale, result, toScale):
    print(str(temperature) + str("° ") + fromScale + " is " + str(result) + "° " + toScale)


def main():
    choice = get_choice()
    if choice == "C" or choice == "c":
        process_celsius ()
    elif choice == "F" or choice == "f":
        process_fahrenheit ()
    else:
        print("You must enter C to convert to Celsius or F to convert to Fahrenheit.")


main()

Output
Enter C to convert to Celsius or F to convert to Fahrenheit:
 c
Enter Fahrenheit temperature:
 100
100.0° Fahrenheit is 37.77777777777778° Celsius

Enter C to convert to Celsius or F to convert to Fahrenheit:
 f
Enter Celsius temperature:
 100
100.0° Celsius is 212.0° Fahrenheit

Enter C to convert to Celsius or F to convert to Fahrenheit:
 x
You must enter C to convert to Celsius or F to convert to Fahrenheit.
Posted
Updated 26-Sep-19 23:57pm
v2
Comments
Richard MacCutchan 26-Sep-19 4:06am    
So what is wrong with what you have tried? And please show us the teacher's example code.
CPallini 26-Sep-19 7:46am    
Please state what is the expected output of your function and, as already suggested by Richard, post here your teacher's sample code.
r_faffie 27-Sep-19 1:14am    
Content moved to question - RM.
Patrice T 27-Sep-19 1:37am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 27-Sep-19 4:37am    
OK, so you still have not explained what your problem is.

1 solution

Look at your code:
Python
print(str(months) + str(days) + str(hours) + str(seconds))
There are a couple of things wrong with that.
Firstly, where are the minutes?

Secondly, if we assume these values:
Python
months = 12
days = 13
hours = 8
minutes = 26
seconds = 59
What does your code print?
Answer: 1213859
Which is - let's be honest here - meaningless.
The code you show in the "example code" about temperatures doesn't just do this:
Python
print(str(temperature) + str(result))
Does it?
It does this:
Python
print(str(temperature) + str("° ") + fromScale + " is " + str(result) + "° " + toScale)

Which adds a lot of additional information that helps the user to understand what you are presenting to them.
So probably what your teacher is saying is: "give me data that I can understand instead of a single number".

But ... you have the whole question he set you, not me!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900