Code Won't Ask User for Input? - python-3.x

I am a beginner in Python programming. Using Python3 for class.
The code I have is:
#!/usr/binpython3
import arduino
def loop():
contin = True
while contin:
userinput = input()
if userinput == "quit":
contin = False
else:
contin = True
I am stuck at the "userinput = input()" portion of my code. For some reason, my program would not ask user for the input. How can I fix this?
Thank you!

Are you actually calling the function? That is, are you saying loop() at the end of your code?

Related

Getting runtime Error for python (Google Code Jam)

I have tried to run my python3 code in google code jam tests however it always gives me a runtime error when testing. I would like it to not give me a runtime error. I have tested on my ubuntu python3.5.3 and it works. I am not sure what causes the runtime error, Is there a way I could get the logs from google code jam or similar?
Here is the code.
if __name__ == "__main__":
testcases = input()
raw = []
while True:
inp = input()
if inp == "":
break
raw.append(inp)
print(raw)
Sorry if this is a newbie question. I am new to code jam and submitting my work online.
This should do the same as what you are trying to do as far as i know. try and lemme know.
If i know what you are trying to take as inputs, i would have been able to give a more specific answer :)
testcases = int(input())
for t in range(testcases):
raw = []
n = int(input())
for i in range(n):
inp = input()
raw.append(inp)
print(raw)

Python 3 How to Ask user for specific input and reject invalid inputs

I have a question on how to check a user's input and make sure they are returning a specific string. Currently, the function when called will ask the user for their input. However, if they choose a string that is not part of the function, the else statement will execute and continue the code. I am trying to figure out how to loop this function, until a user inputs one of the strings that the function is looking for. Can anyone help me with this? I am new to python and would appreciate any help.
def dwarf_class_definer(dwarf_class):
if dwarf_class == "Thane":
print("'Ahhh Nobility'")
elif dwarf_class == "Mekanik":
print("'Interesting a Mechanic'")
elif dwarf_class == "Ancestrite":
print("'A spiritualist. I see...'")
elif dwarf_class == "Prisoner":
print("'Never met a gen-u-ine 'Last chancer.'")
elif dwarf_class == "Civilian":
print("'ehhh a civilian? Wut you doing here?'")
else:
print("You aren't choosing a valid class.")
dwarf_class = input("Which Class will you choose?: ")
dwarf_class_definer(dwarf_class)
A while loop will keep going until you tell it not to anymore. You can see when an expected value is supplied, the break command will terminate the while loop. A dictionary can also make your code a lot cleaner and easier to maintain compared to a bunch of if statements.
dwarf_classes = {
"Thane": "'Ahhh Nobility'",
"Mekanik": "'Interesting a Mechanic'",
"Ancestrite": "'A spiritualist. I see...'",
"Prisoner": "'Never met a gen-u-ine 'Last chancer.'",
"Civilian": "'ehhh a civilian? Wut you doing here?'",
}
while True:
dwarf_class = input("Which Class will you choose?: ")
if dwarf_class in dwarf_classes.keys():
print(dwarf_classes[dwarf_class])
break
print("You aren't choosing a valid class.")
example:
$ python3 so.py
Which Class will you choose?: Python!
You aren't choosing a valid class.
Which Class will you choose?: Prisoner
'Never met a gen-u-ine 'Last chancer.'

Function that to exit from other function

I wanted to create to a function which, when called in other function it exits the previous function based on the first function's input.
def function_2(checking):
if checking == 'cancel':
# I wanted to exit from the function_1
def function_1():
the_input = input("Enter the text: ")
function_2(the_input)
I wanted the code for the function_2 so that it exits from function_1, I know that I can put the if statement in function_1 itself but ill use this to check more than one in input in the same function or even in different function I cant put the if block everywhere it will look unorganized so i want to create a function and it will be convenient to check for more than one word like if cancel is entered i wanted to exit the programm if hello is entered i wanted to do something, to do something its ok but to exit from the current function with the help of other function code please :) if any doubts ask in the comment ill try to give you more info im using python 3.x.x on Windows8.
Why not simply:
def should_continue(checking):
if checking == 'cancel':
return False
return True
def function_1():
the_input = input("Enter the text: ")
if not should_continue(the_input):
return
This is the best solution I think.
Another alternative is to raise an Exception, for example:
def function_2(checking):
if checking == 'cancel':
raise KeyboardInterrupt
def function_1():
the_input = input("Enter the text: ")
function_2(the_input)
try:
function_1()
except KeyboardInterrupt:
print("cancelled by user")

how do I break while loop by raw_input()? python 3

I need some help understanding the differences between the following. In the first example, I want the loop to break when the user inputs False:
true = True
while true:
print("Not broken")
true = input("to break loop enter 'False' ")
There was a question asked at:
how do I break infinite while loop with user input
Which gives this solution:
true= True
while true:
print("Not broken")
true = input("to break loop enter 'n' ")
if true == "n":
break
else:
continue
And I don't understand why the first method doesn't work and the second does??? Why doesn't python take the input as if someone was changing the script and change the variable "true"? Whats going on behind the scenes?
Any help would be appreciated. Thanks in advance :)
The while statement is conditional, and the user entering the String "False" will still resolve to a True outcome.
For an idea of what Python considers True and False, checkout this link: https://realpython.com/python-conditional-statements/
Building on this answer Converting from a string to boolean in Python?, the best way to check is:
true = True
while true is not 'False':
print("Not broken")
true = input("to break loop enter 'False' ")

Scopes within while loops

from random import randint
isRunning =True
while isRunning:
dice1 = randint(1,7)
dice2 = randint(1,7)
print("The first die landed on ℅d and the second landed on ℅d." ℅ (dice1,dice2))
user_input = input("Contiue? Yes or No?\n>")
if user_input == "Yes" or "yes":
print("="*16)
elif user_input == "No" or "no":
isRunning = False
I feel like I'm making such a simple mistake and when I decided to look into global variables and what not it still doesn't help. Could anyone explain why the while loop doesn't terminate, although the variable was set to false.
if user_input== "Yes" or "yes" should be
if user_input== "Yes" or user_input =="yes", alternatively it's equivalent to if any(user_input==keyword for keyword in["Yes", "yes"]):
your original if clause is splitting to
if user_input=="Yes" or if "yes" and if "yes" always true, therefore your if-elseif clause always go to if clause.

Resources