0

Python game

from gtts import gTTS from playsound import playsound import time import random numbers=[1,2,3,4,5,6,7,8,9,10,11,12] def sum(): global answer x=random.choice(numbers) y=random.choice(numbers) x_me=x y_me=y text=('plus') text_main=str(x_me)+text+str(y_me) tts=gTTS(text=text_main,lang='en') tts.save('output.mp3') playsound('output.mp3') print(x ,'+', y) answer=x+y score = 0 sum() answer_user=input() print(answer) if answer==answer_user: print('COORECT!!') score+=1 else: print('NOPE SORRY!!') When I run the code it will always show me' NOPE SORRY!!' How can I fix it??

24th Sep 2023, 12:05 PM
Fateme Biglari
Fateme Biglari - avatar
1 Réponse
+ 1
The variable "answer" is integer and "answer_user" is a string. When compares always return False. And it is not recommended to use a built-in function name such as "sum" be your function name because it will override the default sum function. A side tip, you can initialize "numbers" with this code: numbers = list(range(1, 13)) If you print it and check the data type, it is the same as yours, yet the code is much shorter. Also you can use "return" the "answer" to a variable, such as: random_answer = sum() # Once again, a custom function name like "sum" is not recommended Then you can compare it to other variable without using global variable, which also is not advised.
24th Sep 2023, 3:05 PM
Wong Hei Ming
Wong Hei Ming - avatar