ex Des
ex Des

Reputation: 39

input give me incorrect even im writing correct answer

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..

Upvotes: 0

Views: 45

Answers (1)

JRiggles
JRiggles

Reputation: 6760

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

Upvotes: 3

Related Questions