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
Ryley Hamill
161 PointsScript not running
After checking the other questions posted and trying fixes that others had the same issue, I still can't run the script. It simply returns the line in the console.
Here's my code:
user_string = input("Whats your word? ")
user_num = input("Whats your number? ")
try:
our_num = int(user_num)
except:
our_num = float(user_num)
if not '.' in user_num:
print(user_string[our_num])
else:
ratio = round(len(user_string)*our_num)
print(user_string[ratio])
I hope I'm not forgetting anything, thanks for the help!
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsHey Ryley, your code works correctly. Not sure how you were trying to run it.
If you save your code to a file, such as, exception_test.py. Then you can run code using the Python interpretor:
python exception_test.py
Otherwise, you can also run it interactively in the Python interpretor:
$ python3
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> user_string = input("Whats your word? ")
Whats your word? RyleyHamill
>>> user_num = input("Whats your number? ")
Whats your number? 0.75
>>> try:
... our_num = int(user_num)
... except:
... our_num = float(user_num)
...
>>> if not '.' in user_num:
... print(user_string[our_num])
... else:
... ratio = round(len(user_string) * our_num)
... print(user_string[ratio])
...
i
>>>
Josh Keenan
20,315 PointsWhat challenge is this for?
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsAs a code critique, though syntactically equivalent,
if '.' not in user_num:is preferred over:
if not '.' in user_num: