For some context, I'm working on a farenheit to celsius code converter, I also added some extra games and modifications to the code. But the variable "ContinueYN" will get the "yes" and "no" input, but will not get the "I don't understand input".
This is my code:
import random
import time
continueYN = "y"
firstletter = "n"
firstletterCAP = "N"
firstletterY = "y"
firstletterCAPY = "Y"
Revert = "I dont understand"
def Modification():
x = random.randint(1,100)
print("Calculate",x,"Fahrenheit to Celsius, The Formular is","(",x," - 32) * 5 / 9, You Have 30 Seconds, Your Time Starts Now!")
useranswer = int(input())
time.sleep(30)
answer = (x - 32) * 5 / 9
if useranswer == answer:
print("You Did It!")
quit()
else:
print("Try Again!")
while continueYN == "y":
sDegreeF = input("Enter next temperature in degrees Fahrenheit (F):")
nDegreeF = int(sDegreeF)
nDegreeC = (nDegreeF - 32) * 5 / 9
print ("Temperature in degrees C is:", nDegreeC)
if nDegreeC < 0:
print ("Pack long underwear!")
if nDegreeF > 100:
print ("Remember to hydrate!")
continueYN = input("Input another?")
if continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY:
Modification()
if continueYN[0] == firstletter or firstletterCAP:
break
if continueYN == Revert:
continueYN = input("Input another?")
else:
print("I don't understand!")
I am using python 3.10.5, I would appreciate an answer as well as an explanation on what I was doing wrong
CodePudding user response:
Simple syntax error, you missed the second condition on if statement
if continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY:
Modification()
if continueYN[0] == firstletter or continueYN[0] == firstletterCAP:
break
if continueYN == Revert:
continueYN = input("Input another?")
else:
print("I don't understand!")
your if statement should be like this
CodePudding user response:
Although it becomes confusing as to what you are trying to achieve as I try to understand the functionality of your code. Also your question is a bit unclear too. Still I try to answer considering, what you want to achieve is, "If a user inputs anything that does not start with "y","Y","N","n", the program should display: I don't understand!": Your code has below issues relating to that:
You are not comparing the value in second fi statement, rather you have just put the value which will always be true:
Wrong:
if continueYN[0] == firstletter or firstletterCAP:
break
Right:
if continueYN[0] == firstletter or continueYN[0] ==firstletterCAP:
break
You are comparing the string value to the output you need rather than what user provides. Use below piece of code in place of your conditional statements:
if continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY:
Modification()
if continueYN[0] == firstletter or continueYN[0] ==firstletterCAP:
break
else:
print("I don't understand!")
continueYN = input("Input another?")
Also, the condition that you have for while loop will not be satisfied in all positive scenario. Please replace it with below:
while(continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY):
CodePudding user response:
You don't really need 'yYnN' style of responses to decide if the input and consequential calculations should continue. An empty input could be used to indicate that no more input is required whereas anything else is assumed to be a Fahrenheit value.
You should always validate user input.
This code ignores the OP's Modification() function as its purpose is unclear unless it's giving the user a 1 in 100 of selecting the same random number as returned by randint(). Pretty pointless.
Anyway...
while True:
fs = input('Enter temperature in Fahrenheit (Return to finish): ')
if fs:
try:
f = float(fs)
c = (f - 32) * 5 / 9
print(f'The temperature in celcius is {c:.2f}')
if c < 0:
print('Pack long underwear!')
elif f > 100:
print('Remember to hydrate!')
except ValueError as e:
print(e)
else:
break