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


Messages In This Thread
Use of input function to change screen background color in Turtles - by Oldman45 - Jul-09-2020, 02:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,036 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,018 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,095 Dec-25-2022, 03:00 PM
Last Post: askfriends
  simplekml change shape&color issac_n 2 2,844 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  Showing an empty chart, then input data via function kgall89 0 981 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,137 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  input function question barryjo 12 2,725 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,140 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,103 Dec-04-2021, 12:16 AM
Last Post: luilong
  Exit function from nested function based on user input Turtle 5 2,918 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