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 trial

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