0

I'm trying to make quiz game but if i answered correct answer it gives me incorrect , is it case sensitive ?

Code :

score = 0
question_n = 0

playing = input('Do you wish to start? (Yes-No)')
if playing == "yes" or playing == "y":
    question_n +=1
    ques = input(f'\n{question_n}. Who owns amazon? ')

    if ques == 'Jeff bezos':
        print('Correct! You got 1 point.')
        score +=1
        print(f'Your score is : {score}')
                 

    else:
        print('Incorrect')
        print(f'The correct answer is --> Jeff bezos. ')
        x = input('Press any key to exit..')
elif playing == "no" or playing == "n":
    print('Thanks for trying me (:')

Error :

Do you wish to start? (Yes-No)yes

1. Who owns amazon? jeff bezos
Incorrect
The correct answer is --> Jeff bezos. 
Press any key to exit..
JRiggles
  • 4,847
  • 1
  • 12
  • 27
ex Des
  • 39
  • 5
  • This doesn't appear to be a `tkinter` question – JRiggles Jan 23 '23 at 17:38
  • Yes it's case sensitive. taking some string `s = 'jEfF bEzOs'` and setting `s = s.title()` will set `s = 'Jeff Bezos'` by capitalizing the first letter of every word in your string. – Vin Jan 23 '23 at 17:45
  • 2
    Does this answer your question? [How do I do a case-insensitive string comparison?](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) – Ignatius Reilly Jan 23 '23 at 18:08

1 Answers1

3

Yes, comparison of strings is case-sensitive. You could make it case-insensitive by performing the same case modification on both the input string and the comparison string, e.g.:

if ques.upper() == 'Jeff bezos'.upper():
    ...

In the example above, both the user input and the "correct" string are converted to uppercase before comparison. This way, if the user inputs "JeFf BeZoS" and the correct answer is "Jeff bezos", they are both compared as "JEFF BEZOS" and therefore considered equal.

There are functions for working with string cases in Python

  • str.upper() # convert to upper case
  • str.lower() # convert to lower case
  • str.capitalize() # capitalize string
  • str.title() # capitalize every word in the string (thanks @Vin, I was unaware of this one!)

Examples:

string = 'foo'
print(string.upper())
>>> FOO
string = 'FOO'
print(string.lower())
>>> foo
string = 'foo bar'
print(string.capitalize())
>>> Foo bar
string = 'foo bar'
print(string.title())
>>> Foo Bar
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • 1
    And there's [`casefold`](https://docs.python.org/3.8/library/stdtypes.html#str.casefold), which [can help](https://stackoverflow.com/a/29247821/15032126) with names like Gauß :) – Ignatius Reilly Jan 23 '23 at 18:23
  • @IgnatiusReilly That's cool! Man, I learn something new every day around here. Thanks for the info! – JRiggles Jan 23 '23 at 18:25