import sys
import turtle
t=turtle.Pen
def what_to_draw():
print ("What to do you want to see a sketch of that may or may not be colored?")
what_to_draw=sys.stdin.readline()
if what_to_draw=="flower/n":
t.forward(90)
elif():
print ("What you typed isn't in the code. Try putting a letter or letters to lowercase or uppercase. If that doesn't work, what you typed has not been set to make something happen")
I typed in this code above. It says in the python shell "flower" isn't defined. Can someone figure this out for me?
Several lines of your code have errors of some sort:
t=turtle.Pen should be: t = turtle.Pen()
You should avoid functions and variables with the same name
def what_to_draw():
...
what_to_draw = sys.stdin.readline()
Use rstrip() to deal with "\n" and .lower() to deal with case:
if what_to_draw == "flower/n":
elif(): requires a condition of some sort otherwise use else:
Let's try a different approach. Instead of mixing console window input with turtle graphics, let's try to do everything from within turtle using the textinput() function that's new with Python 3 turtle:
from turtle import Turtle, Screen
def what_to_draw():
title = "Make a sketch."
while True:
to_draw = screen.textinput(title, "What do you want to see?")
if to_draw is None: # user hit Cancel so quit
break
to_draw = to_draw.strip().lower() # clean up input
if to_draw == "flower":
tortoise.forward(90) # draw a flower here
break
elif to_draw == "frog":
tortoise.backward(90) # draw a frog here
break
else:
title = to_draw.capitalize() + " isn't in the code."
tortoise = Turtle()
screen = Screen()
what_to_draw()
screen.mainloop()
Your indentations are wrong, so most of the statements are outside the function body of what_to_draw().
You don't actually call the function, so it won't do anything.
Also, don't use the what_to_draw as a function name and a variable name.
There shouldn't be () after elif:
Instead of print() and stdin, use input() . You don't get the \n that way as well.
Try this and let me know:
import sys
import turtle
t=turtle.Pen()
def what_to_draw():
draw_this = input("What to do you want to see a sketch of that may or may not be colored?")
if draw_this == "flower":
t.forward(90)
elif:
print ("What you typed isn't in the code. Try putting a letter or letters to lowercase or uppercase. If that doesn't work, what you typed has not been set to make something happen")
what_to_draw()
Related
So, basically, I was just coding so that if you write a game's name, it opens it.
print('You can play games here!')
time.sleep(1.5)
print('Snake')
time.sleep(0.5)
print('Maze')
time.sleep(1.5)
print('Type the name of one of the games to open it!')
game = input()
if game == 'Snake':
import Snake
else:
if game == 'Maze':
import Maze
print('This Program Is Still In Development')
When I choose "Snake", it works perfectly. But when I choose "Maze", it just skips to the print('This Program Is Still In Development') part.
Can anyone help, please? I am on MAC OS X btw
EDIT: If I do elif, it doesn't change. Neither does a function. For some reason the only game that works is Snake.
EDIT 2: The weird thing is that when I just open Maze.py separately, IT WORKS. HOW?
change this:
else:
if statement:
# code
into this:
elif statement:
# code
this works (added print to see its working):
import time
print('You can play games here!')
time.sleep(1.5)
print('Snake')
time.sleep(1.5)
print('Maze')
time.sleep(1.5)
game = input('Type the name of one of the games to open it!')
# lowercase the user input to have less conditions
# and to make sure that we skip conditions because of case sensitivity
game = game.lower()
if game == 'snake':
# do stuff
print("snake")
elif game == 'maze':
# do stuff
print("maze")
print('This Program Is Still In Development')
I'm making a text based game and my text currently types out letter by letter but for one specific scenario during the game I want it to show up all at once.
This is the part of the code that allows it to show up letter by letter
import time
import sys
import keyboard
def print(s):
for c in s:
if keyboard.is_pressed("ctrl"):
sys.stdout.write(c)
sys.stdout.flush()
else:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.025)
it also allows the user to press CTRL while the text is showing up to immediately show the rest of the text so they don't have to wait.
Now anytime I use print like this
print ("sample text")
the text show up letter by letter which is fine for the majority of the game except for this part
print ("sample text")
input("\n[Press enter to continue]\n")
print ("*KNOCK*")
Ideally I want the knock to show up immediately.
If I use an input like this
input ("*KNOCK*")
the text does show up immediately but the user has to press enter to get the next part.
Ideally I just want it to move on to the next print section automatically
What would be my best solution?
Don't call it print, since that overwrites the built-in print function. Just call it something else and then you'll be able to use print to show things instantly. You could name it something like print_characters_individually.
I'm a beginner with python and turtle. I want to make a dialogue box that asks a yes or no question. While I can get the box to pop up, how would I code it so that a "no" would close the turtle program and "yes" would keep it up? The part below the screen.textinput is wrong but I had it before for the terminal, and I imported turtle above.
screen = turtle.getscreen()
screen.textinput("Welcome to Bowling!", "Are you readt to bowl?!")
if start.lower() == 'yes':
print("Start!")
else:
print("Goodbye!")
turtle.clear
turtle.bye()
This should get you started:
import turtle
screen = turtle.Screen()
answer = screen.textinput("Welcome to Bowling!", "Are you ready to bowl?")
if answer is None or answer.lower().startswith('n'):
print("Goodbye!")
screen.clear()
screen.bye()
else:
print("Start!")
The key thing to remember is that textinput() returns the string the user typed or None if the user hits Cancel.
This is an alternative to cdlane's solution:
import turtle
screen = turtle.Screen()
answer = turtle.simpledialog.askstring("Welcome to Bowling!", "Are you ready to bowl?")
if answer is None or answer.lower().startswith('n'):
print("Goodbye!")
screen.clear()
screen.bye()
else:
print("Start!")
Note: I have not tested this code yet, I just tweaked cdlane's code
There are other options to the simpledialog function.
There are the askinteger and askfloat for integers and floats respectively. I haven't fully understood these as I just found the function while exploring around with the code prediction of VSCode
I want to use python turtle to create a program that asks the user how many sides they want on a polygon, then turtle draws it. Here is my code:
import turtle
window = turtle.Screen()
window.bgcolor("lightgreen")
shape = turtle.Turtle()
shape.color("blue")
shape.pensize(3)
sides = int(input("How many sides do you want? Use digits: "))
def polygon(sides,length):
for x in range(sides):
shape.forward(length)
shape.left(360/sides)
For some reason this won't work. Any ideas?
Thanks in advance!
You don't actually call polygon, you only define it (that's what the def part of def polygon(sides,length): means.
Try adding something like
polygon(sides, length)
to the bottom of your script; or more specifically anywhere after the definition.
Original
If you're using Python 2 you should probably use raw_input instead of input.
Other than that, try and include the error message / output to receive a moore targeted answer.
The only reason i can see why it doesn't work is that you haven't put in the final line of code in. This is actually essential or python wont run the def. For your instance it would be: polygon(sides,100)
I only put the 100 in as an example and you can change it yourself to whatever you desire
You need to call the function you create like this:
polygon(sides,100)
Or any other length you want instead of 100
The other thing you can do is to ask user to enter the length from console
length = int(input("How tall do you want the lines? Use digits: "))
Your code should look like this
import turtle
window = turtle.Screen()
window.bgcolor("lightgreen")
shape = turtle.Turtle()
shape.color("blue")
shape.pensize(3)
sides = int(input("How many sides do you want? Use digits: "))
def polygon(sides, length):
for x in range(sides):
shape.forward(length)
shape.left(360/sides)
polygon(10, 90) # replace these numbers with the values that you want
The first thing I saw was you didn't have a space between the two variables in the def() statement. The second thing is that when you create a def it is like creating a big variable where all the code inside is that variable(kinda) the variables inside the parenthesis in the def statement are variables that need to be defined later on when calling the function so that the function will run correctly!
I have written a script simulating a card game in Python where the user decides how many cards and how many piles of cards they want to play with. This input is controlled by following code where boundary_1 and boundary_2 give upper and lower limit in an integer interval and message is the user input:
def input_check(boundary_1, message, boundary_2):
run = True
while run:
try:
user_input =int(input(message))
if boundary_1 <= user_input <= boundary_2:
run = False
return user_input
else:
print ("Incorrect Value, try again!")
run = True
except ValueError:
print ("Incorrect Value, try again!")
I now want to try and make a GUI out of this card game using tkinter and I would therefore like to know if there's any way to save the user's input to a variable that could be sent into the input_check() function above? I've read through some tutorials on tkinter and found the following code:
def printtext():
global e
string = e.get()
text.insert(INSERT, string)
from tkinter import *
root = Tk()
root.title('Name')
text = Text(root)
e = Entry(root)
e.pack()
e.focus_set()
b = Button(root,text='okay',command=printtext)
text.pack()
b.pack(side='bottom')
root.mainloop()
The following code simply prints the user's input in the Textbox, what I need is the user's input being checked by my input_check() and then have an error message printed in the Textbox or the input saved to a variable for further use if it was approved. Is there any nice way to do this?
Many thanks in advance!
The simplest solution is to make string global:
def printtext():
global e
global string
string = e.get()
text.insert(INSERT, string)
When you do that, other parts of your code can now access the value in string.
This isn't the best solution, because excessive use of global variables makes a program hard to understand. The best solution is to take an object-oriented approach where you have an "application" object, and one of the attributes of that object would be something like "self.current_string".
For an example of how I recommend you structure your program, see https://stackoverflow.com/a/17470842/7432