Why dont my functions work? - python

This is python 3, this code basically checks if a word is the same when read backwards. When i execute this through Visual Studio, nothing happens, and I get the prompt to press any key to continue...
if "__name__" == "__main__":
StartProgram()
def StartProgram():
Input = AskForDataSimple()
print(CheckIfPalindrome(Input))
def AskForDataSimple():
print("Please input the line to test.")
In = input()
return In
def CheckIfPalindrome(x):
if x[::-1] == x:
return True
else:
return False
Please note that this simpler version actually works:
x = input()
if x[::-1] == x:
print(True)
else:
print(False)

if "__name__" == "__main__":
Change this to
if __name__ == "__main__":
__name__ is a variable containing name of this module. You need these line so that your main logic would be used only if this file is executed directly, not when imported as a module by another code.
Still it won't work, because you need to define the function you call before these lines: move these lines to the end of the file.
Also, this
def CheckIfPalindrome(x):
if x[::-1] == x:
return True
else:
return False
can be replaced with
def CheckIfPalindrome(x):
return x[::-1] == x

Move main function to bottom of file and try it
if __name__ == "__main__":
StartProgram()

Related

Python: How to determine if a String can be converted to an integer with no issues

I am writing a program that has multiple functions to execute, and the user selects which one runs by inputting a number. I also want the user to be able to let the user cancel the request by typing "cancel".
Right now this is my code:
func = input("Requested Operation: ")
if func == 'Cancel' or func == 'cancel':
break
elif func == '' or func == ' ' or func == '0':
func = 0
elif type(int(func)) is int:
func = int(func)
else:
fail = True
Context: Function 0 displays a list of the available items to choose from, so I want whitespace or 0 to work as displaying the project list. If the user types "Cancel" or "cancel" it will end the program.
The problem I am having is line 6 (the 2nd elif). My goal is to set the fail variable to True if the user inputs a string that isn't a cancel command, so the code breaks right there and starts over. The problem is, how do I preemptively check if a string can be converted to an integer in the first place? My current iteration returns the error invalid literal for int() with base 10: 'asdg' (asdg being the random nonsense that should make fail = True).
Also, I understand this method is probably super inefficient. Essentially, I want the conditional to be "if func is cancel, break. If func is whitespace or '0', then it equals 0. If func is some non-0 integer, convert the string to an integer and continue. Otherwise, set fail to True and break."
My knowledge of python is minimal so I would very much appreciate a full explanation or link to documentation so I can learn as much as possible.
Thanks in advance :)
Edit: This is the entire module
import projects.dice_app as dice_app
import projects.text_to_math as text_to_math
def main():
f = open("readme_files/index.txt")
p = open("readme_files/projects.txt")
print(f.read())
func = 0
while True:
fail = False
func = input("Requested Operation: ")
if func == 'Cancel' or func == 'cancel':
break
elif func == '' or func == ' ' or func == '0':
func = 0
elif type(int(func)) is int:
func = int(func)
else:
fail = True
break
if func == 0:
p = open("readme_files/projects.txt")
print(p.read())
elif func == 1:
dice_app.dice_func()
elif func == 2:
text_to_math.ttm_func()
else:
print("Invalid operation. Please try again.")
if __name__ == "__main__":
fail = False
main()
while fail == True:
main()
elif func.isnumeric():
func = int(func)
try :
func = int(func)
except ValueError:
print('not a number')
This should work

Calling a method from farther down the stack

Ok I have a code that's basically this....
def create():
#long list of print statements
m_commands()
def get_desc():
#print statement
m_commands()
def m_commands():
while close != True:
input = input(str(">>>"))
If inp == "close" or "quit":
close = True
If inp == "create":
create()
If inp == "describe":
get_desc()
m_commands()
I need to call m_commands() in create() and get_desc() to continue continuity. Is this possible. Sorry in advance, I don't know how to put code in a spoiler thingy. Thanks!
def create():
#long list of print statements
print("hello")
m_commands()
def get_desc():
#print statement
print("world")
m_commands()
def m_commands():
close=False
while close != True:
inp = input(str(">>>"))
if inp == "close" or "quit":
close = True
if inp == "create":
create()
if inp == "describe":
get_desc()
m_commands()
This is working for me though.

Return value if string has x character

The program asked is:
"besides testing if the length of the given string is more than ten characters, it also tests if there is the character "X" (capital X) in the given string. If the string is longer than 10 characters and it has X in it, the tester subfunction returns a value True to the main function, otherwise False.
If the subfunction returns True to the main function, the program prints "X spotted!". As earlier, if the user inputs "quit", the program terminates."
This is what I tried, but the part of checking the x character does not work at all
def check(st,res="Too short"):
if len(st)>=10:
if checkX(st):
st=st+"\nX spotted!"
return st
else:
return res
def checkX(st):
for i in st:
if i=="X":
return True
return False
def main():
while True:
st=input("Write something (quit ends): ")
if st=="quit":
break
print(check(st))
It only checks if introduced string length is equal or higher than 10 characters.
The code works.
What should you change in your code:
You can use the in operator:
if "blah" not in somestring:
continue
Does Python have a string 'contains' substring method?
Do a real "main":
if __name__ == '__main__':
What you can do if you want to keep the main() function:
A module can discover whether or not it is running in the main scope by checking its own name, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:
if __name__ == "__main__":
# execute only if run as a script
main()
main — Top-level script environment
Result (this code works, it is simplier, more pythonic and respect pep8):
def check_input(tested_sentence: str, result: str = "Too short"):
if len(tested_sentence) >= 10:
if "X" in tested_sentence:
result = tested_sentence + "\nX spotted!"
else:
result = tested_sentence
return result
def main():
while True:
sentence = input("Write something (quit ends): ")
if sentence == "quit":
break
print(check_input(sentence))
if __name__ == '__main__':
main()
Code Style — The Hitchhiker's Guide to Python

def() function screws with my indentation

I'm writing a basic script that scans files and moves them if they fit some criteria set by the user.
I have tried complying with what it wants but that just ends up with every line of code being indented one more than the other.
def check():
#code simplication
for i in range(2):
if fileScanDestination == '':
noValue(moveFrom)
else:
#exits
if fileMoveDestination == '':
noValue(moveTo)
else:
#exits
if specialFileExtension == '':
str(specialFileExtension) == 'nil'
else:
#exits
if fileNameSpecial == '':
str(fileNameSpecial) == str('nil')
else:
#exits
def p(text):
print(text)
#setting up tkinter
# getting variable data
p('Please enter the path for the files you would like to sort.')
fileScanDestination == str(input())
This should just have to be indented once, then exit. But since it wants to indent every new line, which just looks bad.
Try putting some pass instructions after the else statements, or some return's if you say it should exit.
You could even shorten the whole thing by doing:
if any([
fileScanDestination == '',
fileMoveDestination == '',
specialFileExtension == '',
str(specialFileExtension) == 'nil',
str(fileNameSpecial) == str('nil'),
fileNameSpecial == ''
]):
return

python calling definition by variable name

a python beginner here. My previous programming experience is with basic in the eighties, and logic programming in a proprietary system, neither of which is much help for learning python. So, to my question:
I'm writing a math quiz program (just for learning), and I've made a "main menu" by defining a function block; within it, if input is a then another func addition() is called, if input is s then func subtraction() is called and this works as intended. Within those function blocks, I'm setting a global variable quiztype to name of that function. Then I call yet another function again() from within those, to query if user wants another question of the same sort, if yes, I try to return to the relevant function with quiztype () and this fails with TypeError: 'str' object is not callable.
I did find some seemingly-related topics but either couldn't implement the answers or didn't even understand what they were talking about as I'm a beginner.
What options do I have for returning to the previously executed function?
Here's the code: (notice that variable names are not what above - different language)
from random import randint
def Alku ():
kysy = True
while kysy:
lasku = input('Yhteen, Vähennys, Lopeta? ')
if lasku == 'y':
Yhteenlasku ()
kysy = False
elif lasku == 'l':
break
kysy = False
def Uudestaan ():
kysy = True
while kysy:
samauudestaan = input('uudestaan? (k/e)? ')
if samauudestaan == 'k':
Lasku()
kysy = False
elif samauudestaan == 'e':
Alku ()
kysy = False
def Yhteenlasku ():
global Lasku
Lasku='Yhteenlasku'
n1=(randint(1,10))
n2=(randint(1,10))
a1=n1+n2
print(n1, end="")
print(" + ", end="")
print (n2, end="")
print(" = ", end="")
a2=int(input())
print()
if a1==a2:
print('oikein!')
elif a1!=a2:
print('väärin!')
Uudestaan()
Alku ()
And what is returned in terminal:
Traceback (most recent call last):
File "laskut2.py", line 43, in <module>
Alku ()
File "laskut2.py", line 8, in Alku
Yhteenlasku ()
File "laskut2.py", line 41, in Yhteenlasku
Uudestaan()
File "laskut2.py", line 19, in Uudestaan
Lasku()
TypeError: 'str' object is not callable
Your code is fine as it stands, although your global declaration is in an odd place. Still, remove the inverted comma's around your definition of Lasku which is defining it as a string and it will work.
global Lasku
Lasku=Yhteenlasku
P.S. Welcome back to programming!
In response to your question, globals would normally be declared at the beginning of your code or when the data to define becomes available but in this case you are defining it as a function, so you can't define it until the function has been defined. I guess as long as it works, where it is is fine. Personally, in this case, I'd define it here:
global Lasku
Lasku=Yhteenlasku
Alku ()
We really need to see your code to see what you want to achieve but from the sound of it you want to do something like this. From the question it look like you will be recalling function within functions and returning functions, creating recursions which is not that pythonic and also will eventually throw errors and the other is not really needed in this situation. jedruniu has put really quite a good explanation on function variable assignment too.
Less robust version:
def addition():
pass # Put code here
def subtraction():
pass # Put code here
def menu():
while True:
cmd = input("Addition or subtraction? (a/s): ")
if cmd == "a":
addition()
elif cmd == "s":
subtraction()
menu()
Other version (w/ score):
def addition():
# Put code here
result = True
return result # Will be added to score, so any integer or True/False
def subtraction():
# Put code here
result = True
return result # Will be added to score, so any integer or True/False
def menu():
score = 0
while True:
cmd = input("Addition or subtraction? (a/s/exit): ").strip().lower()
if cmd == "exit":
break
elif cmd == "a":
score += addition()
elif cmd == "s":
score += subtraction()
else:
print("Unknown option...")
# Do something with score or return score
if __main__ == "__main__":
menu()
You can assign function to a variable (because function is in Python first-class citizen), so effectively, for example:
def fun1():
print("fun1")
def fun2():
print("fun2")
def fun3():
print("fun3")
f1 = fun1
f2 = fun2
f3 = fun3
functions = {
"invoke_f1" : f1,
"invoke_f2" : f2,
"invoke_f3" : f3
}
functions["invoke_f1"]()
function_to_invoke = functions["invoke_f2"]
function_to_invoke()
yields:
fun1
fun2
More reading: https://en.wikipedia.org/wiki/First-class_function
In your specific example, modify your Uudestaan function.
def Uudestaan ():
Lasku = Yhteenlasku #Add this line
kysy = True
while kysy:
samauudestaan = input('uudestaan? (k/e)? ')
if samauudestaan == 'k':
Lasku()
kysy = False
elif samauudestaan == 'e':
Alku ()
kysy = False
because you were trying to invoke string, and this is not possible. Try to invoke type(Lasku) in your case and you'll see that it is of type str. Invoke it in function with my modification and you'll see type of function.
However I am not sure what is going on in this code, is this finnish? swedish?

Categories