Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjames fernando
Courses Plus Student 427 Pointsneed help with this game script if i enter a float the game shows an error what should i do
import random
while True: number = random.randint(1,10) number1 = input("Guess a number between 1 and 10 ")
if number1 == "D": break print(number) elif int(number1) > int(10): print("only between 1 and 10")
continue
elif int(number1) < int(1): print("only between 1 and 10") continue elif int(number1) == int(number): print("AWESOME!") print(number) continue
elif int(number1) > int(number): print("wrong your number is a bit hight.") print(number) continue
elif int(number1) < int(number): print("wrong your number is a bit low.") print(number) continue
1 Answer
Kian Tahsini
7,138 PointsYou'll want to round the float, if there is one. Below is the code you should try using. I have used a Try/Except statement for this to test if the number is a float and then round it. Add this under your input for var number1
try:
number1 = int(number1)
except:
number1 = float(number1)
number1 = round(number1)
Or if you want to completely stop the game if a float is entered do this:
try:
number1 = int(number1)
except:
break
Hope this helps.