Setting window and turtle color with input

This is the place for queries that don't fit in any of the other categories.

Setting window and turtle color with input

Postby mickprops » Mon Jan 13, 2014 6:16 am

Hello, I'm an extremely new Python programmer who is following the online interactive 'How to Think Like A Computer Scientist' text at interactivepython.org

I'm currently on Chapter 4 and learning about turtles. After telling you how to import the turtle module and create your turtle, as well as give it a color and pen size, the book asks you to modify a sample program to ask a user for input and to set a few parameters (screen background color, turtle color, turtle pen size) based on that input.

Here's what I tried:

Code: Select all
import turtle

wcolor = input('Please enter a window color')
wn = turtle.Screen()
wn.bgcolor(wcolor)        # use input to set bg color

tesscolor = input('Please enter a color for your turtle')
tesspensize = int(input('Please enter a pen size for your turtle'))
tess = turtle.Turtle()
tess.color(tesscolor)              # use input to set tess' color
tess.pensize(tesspensize)                 # use input to set tess' pen size

tess.forward(50)
tess.left(120)
tess.forward(50)

wn.exitonclick()                # wait for a user click on the canvas


Here's the error message I get:

Code: Select all
Traceback (most recent call last):
  File "C:/Python33/Test1.py", line 5, in <module>
    wn.bgcolor(wcolor)        # use input to set bg color
  File "C:\Python33\lib\turtle.py", line 1229, in bgcolor
    color = self._colorstr(args)
  File "C:\Python33\lib\turtle.py", line 1150, in _colorstr
    raise TurtleGraphicsError("bad color string: %s" % str(color))
turtle.TurtleGraphicsError: bad color string:  red


If I type
Code: Select all
print(wcolor)
into the shell, it returns 'red'. If I set wcolor to 'red' and then run the program.. it works, turning the window red. Somehow, the 'red' I'm getting from the input isn't the same as the 'red' I manually set.

I tried converting the input statement to a string (even though I'm fairly sure input defaults to being a string) with
Code: Select all
wcolor = str(input('Please enter a window color'))
and of course that didn't work.

I'm just quite confused and frustrated by what should seemingly be incredibly simple.

Am I missing something small or do I need to make bigger changes in order to get this to work right? The book makes it seem like a simple matter and thus far has treated me like I know absolutely nothing about programming (which is basically true) so I would think if it wasn't this simple it would provide some kind of additional hint.....

Anywho, thanks for taking the time to look at this. This is my first post so I'm sorry if I did anything annoying!
Last edited by micseydel on Mon Jan 13, 2014 6:37 am, edited 1 time in total.
Reason: Locked OP, change traceback from red to code tags.
mickprops
 
Posts: 4
Joined: Mon Jan 13, 2014 5:46 am

Re: Setting window and turtle color with input

Postby stranac » Mon Jan 13, 2014 10:24 am

Looks like you entered ' red'(leading space) instead of 'red'...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Setting window and turtle color with input

Postby mickprops » Mon Jan 13, 2014 5:32 pm

Oh..... wow. Yeah that would be the problem. Thanks... *slinks away embarrassed*
mickprops
 
Posts: 4
Joined: Mon Jan 13, 2014 5:46 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest