Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Pick a Number! Any Number! The Challenge

need 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

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