Trying something out | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Trying something out

I am trying to add input to this example from the Conditional Statement lesson for Python. #sets the value of age age = 22 if age >= 18: # executed only if customer is over-age print("Regular price") else: #executed only if age is less than 18 print("Discount") I replaced ā€œ22ā€ for ā€œinput()ā€ User gets prompted for age, but always returns an error message. It should display ā€œDiscountā€ if user enters a number under 18 but it always returns an error message.

24th Jul 2023, 7:46 PM
AniZaTo
2 Respostas
+ 6
AniZaTo Input returns a string, and we are comparing a string to an integer. To fix this, we can convert the type of this string to an integer by doing something like this: age = int(input()) If you dont understand try working it inside out, We ask for input then convert that value to an integer. remeber were not checking if the variable is an int were converting it, so if we type a number that cant be converted it will return an error.
24th Jul 2023, 7:55 PM
Junior
Junior - avatar
+ 3
YES! You beautiful person! Thank you! I just needed to convert the string to an integer! LOL so, simple once you see it
24th Jul 2023, 8:18 PM
AniZaTo