0
Loops for a)b)c)d) questions
I have a list of questions ABCD-type with assigned to them answered. code asking for input, and according to users input(a, b, c, d) giving an answer. Ater first time when user choosing letter, code ends and giving Trace Back error. I need to loop it, and let user check all answers. Also, problem with "else" It adds print() from "else" to every answers. x = input() if x == ("a"): print("COP1000") if x == ("b"): print("To convert a string to a floating point number use int()") else: print("Fail")
4 odpowiedzi
0
I think you just need to replace the 2nd if by elif
That way the last else will not run when x=="a"
0
If I understand your problem (I am not sure :/), "Fail" should not be print when x=="a". It is because the if/else structure is made of two completely independent if/else :
- the first one with only an if with the condition x=="a" ;
- the second one with an if and an else with the condition x=="b".
If you want to print Fail if x!="a" and x!="b" then you juste have to replace the second if by elif (which is the python syntax for else if). I hoped I did not answered the wrong question ^^
0
here is whole code:
print("A) What is your full name and the section number of this COP 1000 class?")
print("B) What is the name of the function that converts a string to a floating point number?")
print("C) What is the symbol used to start a Python comment?")
print("D) What is the Python data type used for numbers without decimals?")
print("E) What is the purpose of the \t escape character?")
print("********************************************************************************")
print("Enter A, B, C, D:")
x = input()
if x == ("a"):
print("Introduction to Computer Programming")
if x == ("b"):
print("To convert a string to a floating point number use int()")
if x == ("c"):
print("To start a comment press # key")
if x == ("d"):
print("")
if x == ("e"):
print("\t Insert a tab in the text at this point.")
else:
print("Wrong input")
0
Yes that is what I answered :)