I'm trying to use the input function but every time, without fail, the word "None" appears at the end of my question/string (well running), it's capitalized just like that but has no overall effect on the code, it just ruins the project I'm working on.
Here's my code:
import time
import sys
def dp(s):
for c in s:
if c != " ":
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.22)
elif c == " ":
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0)
name = input(dp("Hello, whats your name? "))
Every function in python will return something, and if that something isn't defined it will be None (you can also explicitly return None in a function). So, your dp() function returns None, and so you are effectively calling input(None), which gives the same prompt of None.
Instead, try putting in input() call within the function itself.
def dp(s):
for c in s:
if c != " ":
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.22)
elif c == " ":
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0)
return input()
name = dp("Hello, whats your name? ")
Related
import sys
import time
def delay_print(s):
for c in s:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.01)
Colour = input(delay_print("Choose a colour(Red/Black):"))
Expected_result:
Choose a colour(Red/Black):
Actual_result
Choose a colour(Red/Black):None
You don't need to provide the return value (None) from delay_print() as the parameter for input(). Just split it into two lines:
delay_print("Choose a colour(Red/Black):")
colour = input()
If you arrange for delay_print to return an empty string then this will do what you need:
from time import sleep
def delay_prompt(s):
for c in s:
print(c, end='', flush=True)
sleep(0.25)
return ''
value = input(delay_prompt('Hello world: '))
import random,sys,time,os
os.system("cls")
def dialogue(passage): # Function for dialogue to type slowly
for letter in passage:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.1)
name = input(dialogue("Hi, WHat is your name?"))
the input would pop up in the terminal as "Hi, WHat is your name?"NONE.
is there any way I can change this to just return the input without it stating NONE where the output should be?
Just add return '' to the end of the dialogue function:
def dialogue(passage):
for letter in passage:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.1)
return ''
name = input(dialogue("Hi, What is your name? "))
I'm trying to use an external function and apply it to a basic user input. Basically the external function (Code 1) makes the text in (Code 2) print out slowly, like a video game dialog. However the first variable called "intro" is not affected by this function, only the "response" variable is. I don't know how to fix that.
Code 1:
import sys,time,os
def typewriter(self):
for char in message:
sys.stdout.write(char)
sys.stdout.flush()
if char !="\n":
time.sleep(0.1)
else:
time.sleep(1)
os.system("cls")
Code 2:
from typewriter import *
intro = input("What is your name?\n")
typewriter(intro)
response = ("Nice to meet you " + intro + ".\n\
My name is Program.")
message = intro and response
typewriter(response)
You almost got it! As it has been mentioned, your typewriter function should be receiving the text it should print. But also, "What is your name?" is now being printed by input, not by your function.
This works:
import sys
import time
def typewriter(message):
for char in message:
sys.stdout.write(char)
sys.stdout.flush()
if char !="\n":
time.sleep(0.1)
else:
time.sleep(1)
intro = "What is your name?\n"
typewriter(intro)
name = input()
response = ("Nice to meet you " + name + ".\n\
My name is Program.")
typewriter(response)
Clearly function typewriter is not taking any argument to process the data. When you have called the typewriter(intro) the first time the message variable is not defined. I think you need to change the typewriter function as follows.
def typewriter(message):
for char in message:
sys.stdout.write(char)
sys.stdout.flush()
if char !="\n":
time.sleep(0.1)
else:
time.sleep(1)
first of all I tried to rename the input in the typewrite function instead of self i wrote message:
import sys,time,os
def typewriter(message):
for char in message:
sys.stdout.write(char)
sys.stdout.flush()
if char !="\n":
time.sleep(0.1)
else:
time.sleep(1)
os.system("cls")
i would also change the second code to this:
from typewriter import *
typewriter("What is your name?\n")
intro = input()
response = ("Nice to meet you " + intro + ".\n\
My name is Program.")
message = intro and response
typewriter(response)
have fun with coding!
This program essentially encodes and decodes a message and code respectively. I only did the decoding part so far. However I keep getting an EOF error even though I made sure to end parentheses, checked my syntax and kept tampering with it. Unfortunately no luck. Anyone know why this error keeps popping up? I would greatly appreciate it. Also I copied both files that i'm using.
from LetterCodeLogic import LCL
def main():
print("Welcome to the LetterCode program")
choice = getChoice()
while choice !=0:
if choice == 1:
#Encode logic...
print()
elif choice == 2:
#Decode logic...
msg = input("Enter your numbers to decode (separate with commas): ")
#send msg to Decode function in LCL class (LetterCodeLogic.py file)
result = LCL.Decode(msg)
print("Your decoded message is: \n" + result)
else:
print("Unknown process...")
print()
choice = getChoice()
print("Thanks for using the Letter Code program")
def getChoice():
c = int(input("Choice? (1=Encode, 2=Decode, 0=Quit): "))
return c
if __name__ == "__main__":
main()
class LCL:
"""Encode/Decode Functions"""
#staticmethod
def Decode(msg):
#separate numbers from msg string (e.g., "1,2,3")
nums = msg.split(",") #produces list of separate items
result = ""
for x in nums:
try:
n = int(x.strip()) #remove leading/trailing spaces...
if n == 0:
c = " "
elif n < 0 or n > 26:
c = "?"
else:
#ASCII scheme has A=65, B=66, etc.
c = chr(n+64)
except ValueError:
c = "?"
result += c #same as: result = result + c
return result
#staticmethod
def Encode(msg):
the "#staticmethod" and "def Encode()" function was empty and that was the end of line parsing error. When I was coding this and ran it, it ran with no problems. So I removed it for the time being.
This question already has answers here:
How do I re-run code in Python?
(9 answers)
Closed 6 years ago.
I want the code below to automatically rerun itself any ideas hwo to do this? Btw I am new to stack overflow and python itself so If I am doing anything wrong on either please let me know, Thanks
import sys
import os
import random
answer_correct_message = random.choice(['Well done', 'Correct answer','Nice one','Thats correct!'])
answer_wrong_message = random.choice(['Unlucky','Thats wrong','Nope'])
random_num_1 = random.randint(1,10)
random_num_2 = random.randint(1,10)
def question_asker_and_answerer():
q2 = input("What is " + str(random_num_1) + " + " + str(random_num_2) + "?")
if q2 == random_num_1 + random_num_2:
the_questions = True
if the_questions == True:
return (answer_correct_message)
else:
return (answer_wrong_message)
else:
the_questions = False
if the_questions == True:
return (answer_correct_message)
else:
print(answer_wrong_message)
print question_asker_and_answerer()
This is not a situation where you are need a program to rerun itself. That sort of requirement is when you want a script to run as a daemon. This is simply a matter of creating a loop
while True:
print question_asker_and_answerer()
There are two problems here:
how to iterate;
how to make sure that the various randomly-chosen variables are different each pass through.
Just looping over the existing function, or getting it to recurse (as in a couple of other answers) solves the first of these problems (actually, recursing really doesn't, since Python doesn't have tail-call elimination, so it will run out of stack eventually).
To solve both of them you need to make the randomly-chosen variables local to the function, and then loop. I have also modified it so it returns the string to print rather than printing it, in the case of a wrong answer (last line of function).
import sys
import os
import random
def question_asker_and_answerer():
answer_correct_message = random.choice(['Well done', 'Correct answer',
'Nice one','Thats correct!'])
answer_wrong_message = random.choice(['Unlucky','Thats wrong','Nope'])
random_num_1 = random.randint(1,10)
random_num_2 = random.randint(1,10)
q2 = input("What is " + str(random_num_1) + " + " + str(random_num_2) + "?")
if q2 == random_num_1 + random_num_2:
the_questions = True
if the_questions == True:
return (answer_correct_message)
else:
return (answer_wrong_message)
else:
the_questions = False
if the_questions == True:
return (answer_correct_message)
else:
return (answer_wrong_message)
while True:
print question_asker_and_answerer()