Python Forum
Use of input function to change screen background color in Turtles
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of input function to change screen background color in Turtles
#1
I am teaching myself Python on Python 3.8 following Think Like a Computer Scientist and having difficulties as I am a newcomer to coding

I have this code, which works:

import turtle

wn = turtle.Screen()
wn.bgcolor("lightgreen")

alex = turtle.Turtle()
alex.color("red")
alex.pensize(3)

alex.forward(50)
alex.left(120)
alex.forward(50)

wn.mainloop()

I want to get user input to change the screen color (wn.bgcolor) and have modified the code to:


import turtle

screen_color = input("Enter color ") #get user input
wn = turtle.Screen()
wn.bgcolor("screen_color") #change color to user selection

alex = turtle.Turtle()
alex.color("red")
alex.pensize(3)

alex.forward(50)
alex.left(120)
alex.forward(50)

wn.mainloop()

This does not work and I get the following error message:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
= RESTART: C:/Users/John/Documents/John''s files/Work/Coding/Think Like a Computer Scientist/Programmes/turtles.py
Enter color blue
Traceback (most recent call last):
File "C:/Users/John/Documents/John''s files/Work/Coding/Think Like a Computer Scientist/Programmes/turtles.py", line 5, in <module>
wn.bgcolor("screen_color")
File "C:\Users\John\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1237, in bgcolor
color = self._colorstr(args)
File "C:\Users\John\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1158, in _colorstr
raise TurtleGraphicsError("bad color string: %s" % str(color))
turtle.TurtleGraphicsError: bad color string: screen_color
>
I am at a loss of what to do and would appreciate any help or advice.

Thanking you in advance
Reply
#2
Quotes make a string. When referencing a variable, do not put it in quotes.

wn.bgcolor(screen_color) # No quotes so it uses the value of screen_color, which is a string
Reply
#3
Hello Tullis

Thanks for the prompt reply.

That certainly got rid of the error message but wn.bgcolor(screen_color) has not activated any change whereas if I remove the screen_color variable from the code and manually insert a color into the wn.bgcolor variable the code all works and the screen colour changes!

Can you please advise?
Reply
#4
Good Morning Stullis

Apologies for getting your name wrong last time.

I don't quite know what changed but when I opened up my programme this morning it worked perfectly, so your simple fix was dead right. Thank you so much and apologies for the confusion
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 928 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 902 Oct-25-2023, 09:09 AM
Last Post: codelab
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  simplekml change shape&color issac_n 2 2,772 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  Showing an empty chart, then input data via function kgall89 0 943 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,062 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  input function question barryjo 12 2,634 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 2,995 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,019 Dec-04-2021, 12:16 AM
Last Post: luilong
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020