Please help


#1

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


#2

Is this a lesson related question? Or is it related to homework?

user = input(" ... ")
if user in 'yY':

elif user in 'nN':

#3

it's a homework related question, did I post it in the wrong place? Apologize if I did..

I've changed the code to what you gave, but it keeps saying that name 'y' is not defined, would you explain to me what's wrong?

thanks in advance!


#4

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.


#5

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

Can you explain how line 2 works?


#6

The in operator is internally iterative. It may be used with any iterable such as a string, a list, a tuple, a set or a range.

for i in range(10):

if letter.upper() in "AEIOU":

It is also used in dictionaries.

for key in object:
    print (key, object[key])

#7

This may be true, but I wouldn't know. It bears further research so you can satisfy your own curiosity.


#8

Ah I see, thank you so much for the explanation!

Can I ask you another question about using turtle to create a barchart or do I need to post the question in the forum? Thanks in advance


#9

has not been explored on CC. Something to bring up as a new topic, perhaps?


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.