Hi everyone I have an issue and I just cant't solve it. I provided the output but unfortunately I couldn't get the output that I want.
This is my code and I want my output to look like in the image that I uploaded:
def celiusToFahrenheit():
print("Celsius\t\t\tFahrenheit")
for c in reversed(range(31,41)):
f=(9/5)*c+32
print(c,"\t\t\t\t",\
format(f,".1f"))
def fahrenheitToCelsius():
print("Fahrenheit\t\t\tCelsius")
for f in reversed(range(30,130,10)):
c=(5/9)*(f-32)
print(f,"\t\t\t\t",\
format(c,".2f"))
The output that I get:
C:\Users\emrea\PycharmProjects\helloworld\venv\Scripts\python.exe C:/Users/emrea/PycharmProjects/helloworld/app.py
Celsius Fahrenheit
40 104.0
39 102.2
38 100.4
37 98.6
36 96.8
35 95.0
34 93.2
33 91.4
32 89.6
31 87.8
Celsius Fahrenheit
40 104.0
39 102.2
38 100.4
37 98.6
36 96.8
35 95.0
34 93.2
33 91.4
32 89.6
31 87.8
Process finished with exit code 0
Just like I've said, you can find the output that I want from the link below. https://i.stack.imgur.com/swcx6.png
If you want to align the values, you can specify lengths and alignments. See https://docs.python.org/3/library/string.html#format-specification-mini-language
Adding to @L3viathan's answer above...
def celiusToFahrenheit():
yield "{:^12} {:^12}".format("Celsius", "Fahrenheit")
yield "-"*25
for c in reversed(range(31, 41)):
f = (9 / 5) * c + 32
yield "{:12} {:>12}".format(c, format(f, ".1f"))
def fahrenheitToCelsius():
yield "{:^12} {:^12}".format("Fahrenheit", "Celsius")
yield "-"*25
for f in reversed(range(30, 130, 10)):
c = (5 / 9) * (f - 32)
yield "{:12} {:>12}".format(f, format(c, ".1f"))
for left, right in zip(celiusToFahrenheit(), fahrenheitToCelsius()):
print(left, "|", right)
Generators would be a good fit for this: Instead of printing inside the functions, yield
their lines:
def celiusToFahrenheit():
yield "Celsius\t\t\tFahrenheit"
for c in reversed(range(31, 41)):
f = (9 / 5) * c + 32
yield "{}\t\t\t\t{}".format(c, format(f, ".1f"))
def fahrenheitToCelsius():
yield "Fahrenheit\t\t\tCelsius"
for f in reversed(range(30, 130, 10)):
c = (5 / 9) * (f - 32)
yield "{}\t\t\t\t{}".format(f, format(c, ".1f"))
Then you can iterate over both at once:
for left, right in zip(celiusToFahrenheit(), fahrenheitToCelsius()):
print(left, "|", right)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.