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 Solution

Shazad Kazi
Shazad Kazi
10,758 Points

The Solution, Converting the player guess into an int?

I assumed that this bit of code would convert the player guess without causing an exception. However, when testing the game the player cannot enter a number with a decimal even if it is a whole number e.g 3.0 for 3

  try:
    player_num = int(guess)
  except:
    print("That's not a whole number!")
    break

so if my guess was 5.5 it would've converted it to 5.

For example if this was typed in the shell?

a = 5.5
int(a)

2 Answers

You'll want to use a float in that case, because the definition of integer is a value without a decimal or fractional value.

Try

a = 5.5
float(a)

More information about this function here.

Hanley Chan
Hanley Chan
27,771 Points

This should work:

  try:
    player_num = int(float(guess))
  except:
    print("That's not a whole number!")
    break