Im trying to make a code where if the user input y the function will be ran again whereas if they enter n they will exit the function. I was able to make it work, but if I try entering the y or n in an uppercase letter, it will cause an error.
Can anyone tell me how to make it so that if the user enter either lower or upper case it wont matter? Thanks!!
Here’s my code:
y = ("y")
Y = ("Y")
n = ("n")
N = ("N")
addanother = eval(input("Do you want to enter another student details? Please enter Y for yes and N for no\n"))
if addanother == y or addanother == Y:
enterAllMarksOfAStudentToStorage(allStudentMarks)
if addanother == n or addanother == N:
return
This code works if the user enter either lower or upper case, but I don’t think having multiple variables for such a simple problem is a good code. Please help me fix this code
Are you working in Python 2 or 3? In Python 2, eval(input()) is raw_input(). Normally, we would not use the eval() function unless we wish to evaluate inputs as executable code. I don’t see that being the case here.
Please repost your code so we can take another look at what you have changed.
I’m using python 3. And thank you, it works now after I remove the eval, my teacher told me to use that because he said that mac might encounter some problem if we only use input without eval.
The code looks like this now:
1 addanother = input(“Do you want to enter another student details? Please enter Y for yes and N for no\n”)
2 if addanother in ‘yY’:
3 enterAllMarksOfAStudentToStorage(allStudentMarks)
4 if addanother in ‘nN’:
5 return