Im trying to write a program that assigns an empty str to the variable myvar. then in a while loop the program asks for the user to type text, anything they want and it will print to the screen until the user enters the text quit which will end the loop and stop the program.
this is what I have
myvar = str("")
while myvar != "quit":
user_input = raw_input()
user_input = myvar
print myvar
Thanks.
how about
for output in iter(raw_input,"quit"):
print output
you're pretty close, but the indentation's off and the logic needs a slight tweak:
myvar = ""
while myvar != "quit":
myvar = raw_input()
print myvar
note that this will also print "quit". I'll leave that as a final exercise to you to figure out how to cut that out.
Related
So I have this while loop which asks user if he/she wants to repeat the program. All works good but I was trying it out and found out that after user repeats the program and when asked second time if he/she wants to repeat, I choose no. But the program still repeats although it prints that program is closing, how can I fix this?
edit: I've fixed it with changing break with return and adding return after main()
def main():
endFlag = False
while endFlag == False:
# your code here
print_intro()
mode, message=get_input()
ui = str.upper(input("Would you like to repeat the program again? Y/N: "))
while True:
if ui == "Y":
print(" ")
main()
elif ui == 'N':
endFlag = True
print("Program is closing...")
break
else:
print("wrong input, try again")
ui = str.upper(input("Would you like to repeat the program again? Y/N: "))
This is because main() function is being called recursively & endFlag is a local variable.
So when you first put 'y' , then another main() is recursively called from the first main() function.
After you put 'n' which ends this second main() and return to the first main which still in a loop with endFlag (local variable) with value as false.
So, need to change,
Either
endFlag variable as global ( i.e. defined outside main function )
Or,
some program exit function in place of break
The thing is that you're doing recursion over here, i.e. you're calling method main() inside main() and trying to break out of it the way you've done is not gonna work (well, you're know it :) )
Second - you don't need a forever loop inside a first loop, you can do it with one simple loop and break.
Here it is:
def print_intro():
intro = "Welcome to Wolmorse\nThis program encodes and decodes Morse code."
print(intro)
def get_input():
return 'bla', 'bla-bla-bla'
def main():
while True:
# your code here
print_intro()
mode, message = get_input()
ui = str.upper(input("Would you like to repeat the program again? Y/N: "))
if ui == "Y":
print(" ")
elif ui == 'N':
print("Program is closing...")
break
else:
print("wrong input, try again\n")
main()
this is what you should do:
(BTW this is a piece of example code)
while True
name = input('Enter name:')
print ('Hi '+ name)
exit = input('Do you want to exit?(Y/N)')
if exit == 'N' or 'n':
break
(Im not sure if i put the indents correctly )
You can apply this concept into your code.
I am trying to make python cipher and decipher text with the Playfair method. But I hit a roadblock because it seems to output "None" with every line out output.
I'd be grateful if anyone tells me why it's doing so.
(This is my first post, so please bear with any mistakes I might have made).
My code:
def cip():
key=input(print("Please Enter Keyword: "))
return key
def inp():
c = int(input(print("1.Cipher text \n2.Exit\n\t>>")))
if c==1:
cip()
else:
exit
inp()
Output:
C:\Users\XYZ\Desktop\Code\Py programs>python -u "c:\Users\XYZ\Desktop\Code\Py programs\Playfair.py"
1.Cipher text
2.Exit
>>
None1
Please Enter Keyword:
NoneTron
The problem is your use of print() in the input() call.
c = int(input(print("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>")))
^^^^^
print() prints its argument, and returns None. input() uses the value of its argument as the prompt, so it's printing None as the prompt.
Just pass a prompt string to input(), don't call print()
c = int(input("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>"))
The problem is when you use input with print in it. Print should be outside.
def cip():
print("Please Enter Keyword: ")
key=input()
return key
def inp():
print("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>")
c = int(input())
if c==1:
cip()
elif c==2:
decip()
else:
exit
inp()
You could also put the string in input(), like this:
c = int(input("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>"))
I have this exercise and the first part of the program was running fine but I must've done something because now when I try to run it will just show None and and nothing seems to be 'wrong'. I don't know enough to even figure out what's wrong.
def main():
"""Gets the job done"""
#this program returns the value according to the colour
def re_start():
#do the work
return read_colour
def read_names():
"""prompt user for their name and returns in a space-separaded line"""
PROMPT_NAMES = input("Enter names: ")
users_names = '{}'.format(PROMPT_NAMES)
print (users_names)
return users_names
def read_colour():
"""prompt user for a colour letter if invalid colour enter retry"""
ALLOWED_COLOURS = ["whero",
"kowhai",
"kikorangi",
"parauri",
"kiwikiwi",
"karaka",
"waiporoporo",
"pango"]
PROMPT_COLOUR = input("Enter letter colour: ").casefold()
if PROMPT_COLOUR in ALLOWED_COLOURS:
return read_names()
else:
print("Invalid colour...")
print(*ALLOWED_COLOURS,sep='\n')
re_start()
main()
The only function you call is main(), but that has no statements in it, so your code will do nothing. To fix this, put some statements in your main() and rerun your code.
I have a question about python 3.4.1
So suppose you have a program like this:
name = input("What is your name?")
print("Hi", name ,'!')
So you get:
.>>> What is your name?
So you type in you James and you get back:
.>>> Hi James!
Then after it prints this little message, it automatically resets, and goes back to:
.>>> What is you name?
Thanks for you time! :D
while True:
name = input("What is your name?")
print("Hi", name ,'!')
Use a while loop
You need some condition to break out of the loop:
while True:
name = input("Enter your name or press q to quit")
if name == "q":
break # if user enters `q`, we will end the loop with this break statement
print("Hi", name ,'!') # or else just print the name
A nice tutorial on while loops.
I'm very new to programming and python. I'm writing a script and I want to exit the script if the customer type a white space.
questions is how do I do it right?
This is my attempt but I think is wrong
For example
userType = raw_input('Please enter the phrase to look: ')
userType = userType.strip()
line = inf.readline()
while (userType == raw_input)
print "userType\n"
if (userType == "")
print "invalid entry, the program will terminate"
# some code to close the app
I know this is old, but this may help someone in the future. I figured out how to do this with regex. Here is my code:
import re
command = raw_input("Enter command :")
if re.search(r'[\s]', command):
print "No spaces please."
else:
print "Do your thing!"
The program you provided is not a valid python program. Because you are a beginner some small changes to you program. This should run and does what I understood what it should be.
This is only a starting point: the structure is not clear and you have to change things as you need them.
userType = raw_input('Please enter the phrase to look: ')
userType = userType.strip()
#line = inf.readline() <-- never used??
while True:
userType = raw_input()
print("userType [%s]" % userType)
if userType.isspace():
print "invalid entry, the program will terminate"
# some code to close the app
break
You could strip all whitespaces in your input and check if anything is left.
import string
userType = raw_input('Please enter the phrase to look: ')
if not userType.translate(string.maketrans('',''),string.whitespace).strip():
# proceed with your program
# Your userType is unchanged.
else:
# just whitespace, you could exit.
After applying strip to remove whitespace, use this instead:
if not len(userType):
# do something with userType
else:
# nothing was entered