I want my function to accept small and capital letters in python - python

If I print a capital letter instead writing everything in small letters, the function doesn´t give me the correct output.
I would like to have the output "good bye", when I write: Quit quit QuIt
This is the Output right now if I take Quit or quit as an Input
Quit
This is not a valid option. Please try again.
quit
good bye
quit = True
​
help = ("""
start- to start the car
stop - to stop the car
quit - to exit
​
""")
​
print(help)
while quit:
operation = input(">")
if operation == "start":
print("Car started")
elif operation == "stop":
print("Car stopped")
elif operation == "quit":
print("good bye")
break
else:
print("This is not a valid option. Please try again.")

Use .lower() function on the input string to transform your text into lowercase.
So use operation = input(">").lower()
Here you can find a small docs

Convert your input to lower case.
operation = input(">").lower()

Related

allow only integers except one specific string

Is there a way to allow only integers except one specific string? I wrote a number guessing game in python and if the player decides to quit the game, the game should break through the user input "quit", while allowing only numbers during the guessing process.
Thank you in advance!
Check if input is digit and not 'quit' then continue game
print("Insert your number")
user_input = input()
if user_input.isdigit():
#continue your game
elif user_input == 'quit':
#quit game
else:
print("Invalid option. Type in a number or 'quit' for exit")
how about just checking for either numeric or 'quit'?
if input.isnumeric():
#proceed as with number input
elif input == 'quit':
#quit
else:
#invalid input

How to fix a while loop that goes on forever in Python?

I am trying to create a simple while loop that will run the commands start, stop, quit, and help. Start, stop, and help will just display some printed text. After they are run, I want it to keep going on to another command. However, on quit I want the whole program to stop.
input_command = input("> ").lower()
while input_command != "quit":
print(input_command)
if input_command == "start":
print("The car is ready! VROOM VROOM!")
print(input_command)
elif input_command == "stop":
print("Car stopped.")
elif input_command == "help":
print("""
start - starts the car
stop - stops the car
quit - exits the program
""")
else:
print("Sorry, I don't understand that...")
You never reassign input command so it only ever takes input once,
input_command = ''
while input_command != "quit":
input_command = input("> ").lower()

Exiting a program from an If/ELSE statement with Python

I am attempting to exit a program without using sys.exit()
The user is asked whether they wish to continue and if they input "Yes" a message saying so is printed and the program continues to run. If they input anything else a message saying they chose to exit is printed and then the program is meant to exit.
def keep_going():
answer = raw_input("Do you wish to continue?")
if answer == "yes":
print "You have chosen to continue on"
else:
print "You have chosen to quit this program"
What I am struggling with is what to add to ELSE to return something to my main which will cause the program to exit and how to go about writing that in code.
If you are so much keen about not using sys.exit() you could directly use raise SystemExit. Well this exception is technically raised when you call sys.exit() explicitly. In this way you don't need to import sys at all.
def keep_going():
answer = raw_input("Do you wish to continue?")
if (answer == "yes"):
print ("You have chosen to continue on")
else:
print "You have chosen to quit this program"
raise SystemExit
This answer will give you the alternate possible ways.
Try this:
def main():
while keep_going():
keep_going()
def keep_going():
answer = raw_input("Do you wish to continue?")
if answer == "yes":
print "You have chosen to continue on"
return True
else:
print "You have chosen to quit this program"
return False
if __name__ == "__main__":
main()
The program will continue calling keep_going() as long as it returns true, that is when a user answers "yes"
An even shorter solution would be to call keep_going() after the "yes" condition:
def keep_going():
answer = raw_input("Do you wish to continue?")
if answer == "yes":
print "You have chosen to continue on"
keep_going()
else:
print "You have chosen to quit this program"
Just, return something, and if that is returned, then let your main function exit, either by falling off the end, by using a return statement, or calling sys.exit()/raise SystemExit.
As an example, I'm here returning a string (a different one based on what the user answered):
def keep_going():
answer = raw_input("Do you wish to continue?")
if answer == "yes":
print "You have chosen to continue on"
return "keep going"
else:
print "You have chosen to quit this program"
return "please exit"
Now, in main, I can test which of these strings keep_going() returned:
def main():
while keep_going() != 'please exit':
# your code here
if __name__ == "__main__":
main()
While strings will work for this purpose, other values are more commonly used for such a task. If keep_going() returned True (instead of "keep going") or False (instead of "please exit"), then main could be written like
def main():
while keep_going():
# your code here
This also reads pretty naturally ("while keep going do your code"). Note that in this case I'm not comparing the return value to something since True and False are truthy variables - and Python's branching control structures (like if and while) know how they work, i.e. there is no need to write keep_going() == True, and indeed it is considered un-pythonic to do so.
You can try this
def keep_going():
answer = raw_input("Do you wish to continue?")
if answer == "yes":
print "You have chosen to continue on"
else:
print "You have chosen to quit this program"
quit()

How to make Python 3.4 input insensitive?

So I am currently learning how to code Python right now. I decided to do a little program where it requires the user to input a word to command the program to do certain things. I have been having the issue though with putting in every variant of capitalization of the word into the code. My question is, how can I prevent this? Any help would be appreciated. Please keep in mind I am learning Python still and may not understand everything. Here is my code:
#This is the main program.
if choice == '1':
print ("Not Availble Yet")
print (" ")
time.sleep(2.5)
main()
#This is if you wish to quit.
if choice =='2':
end = input ("Are you sure you'd like to quit? ")
#These are all the "I'd like to quit" options.
if end == 'yes':
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == 'Yes':
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == 'yEs':
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == ("yeS"):
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == ("YES"):
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
#These are all the "I wouldn't like to quit" options.
if end == 'no':
print ("Continuing Program").lower
time.sleep(2.5)
main()
Thank you!
Lowercase the input:
end = input ("Are you sure you'd like to quit? ")
end = end.lower()
Then check for "yes" and "no" only.
(This is an instance of a general principle called "input normalization", a very common way to simplify programs.)

Why isn't this code repeating? Python 3.3

This is a code that I have used when repeating a sequence I have used but it doesnt seem to be working can anyone see any problems?The code is for a currency converter. Im using Python 3.3
userDoAgain = input("Would you like to use again? (Yes/No)\n")
if userDoAgain == "Yes":
getChoice()
elif userDoAgain == "No":
print("Thankyou for using this program, Scripted by PixelPuppet")
import time
time.sleep(3)
else:
print("Error: You entered invalid information.")
doagain()
Edit,This is the rest of the code:
if userChoice == "1":
userUSD = float(input("Enter the amount of USD you wish to convert.\n"))
UK = userUSD * 0.62
print("USD", userUSD, "= ", UK, "UK")
elif userChoice == "2":
UK = float(input("Enter the amount of UK Currency you wish to convert.\n"))
userUSD = UK * 1.62
print("UK", UK, "= ", userUSD, "USD")
def doagain():
userDoAgain = raw_input("Would you like to use again? (Yes/No)\n")
if userDoAgain == "Yes":
getChoice()
elif userDoAgain == "No":
print("Thankyou for using this program, Scripted by PixelPuppet")
import time
time.sleep(3)
else:
print("Error: You entered invalid information.")
doagain()
Generally speaking, using recursion to handle a repeated control flow in Python is a bad idea. It's much easier, and less problematic to use loops instead. So, rather than defining a function doagain to ensure you get an answer to your question about running again, I suggest using a while loop. For the larger function that you'll be repeating, I suggest using a loop as well.
def repeat_stuff():
while True: # keep looping until told otherwise
# do the actual stuff you want to do here, e.g. converting currencies
do_stuff_once()
while True: # ask about doing it again until we understand the answer
userDoAgain = input("Would you like to use again? (Yes/No)\n")
if userDoAgain.lower() == "yes":
break # go back to the outer loop
elif userDoAgain.lower() == "no":
print("Thank you for using this program")
return # exit the function
else:
print("Error: You entered invalid information.")
Note that I've changed the checks of the yes/no input strings to be case insenstive, which is a rather more user friendly way to go.
You are using recursion (the function calls itself) while it may be much nicer to just wrap the code you want to repeat in a while loop.
Example of this usage:
userContinue = "yes"
while (userContinue == "yes"):
userInput = input("Type something: ")
print("You typed in", userInput)
userContinue = input("Type in something else? (yes/no): ").lower()
Probably you need to use the function "raw_input" instead of only input.

Categories