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
  Input function cutting off commands at spaces. throwaway34 3 319 May-12-2021, 06:40 AM
Last Post: throwaway34
  Defining a function with input abcd 5 591 Feb-21-2021, 02:34 AM
Last Post: NullAdmin
  Function with array input Gerkamspiano 5 484 Dec-01-2020, 08:48 AM
Last Post: Gerkamspiano
  calling a function and argument in an input phillup7 3 609 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Change the color automatically Dragonos 5 847 Jul-28-2020, 01:17 PM
Last Post: Dragonos
  Getting an error while using input function dcsethia 5 762 May-11-2020, 04:59 PM
Last Post: buran
  How to restart Python after input change ozstar 3 815 Apr-29-2020, 03:16 AM
Last Post: ozstar
  Cant define turtle color with function argument Wrightys99 2 621 Apr-22-2020, 01:43 PM
Last Post: Wrightys99
  Sequential color background swisha 2 745 Mar-05-2020, 03:43 PM
Last Post: Larz60+
  simplekml change shape&color issac_n 1 696 Feb-19-2020, 02:50 AM
Last Post: issac_n

Forum Jump:

User Panel Messages

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