Python Forum
Why can't i use def in python? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why can't i use def in python? (/thread-32020.html)



Why can't i use def in python? - celinafregoso99 - Jan-15-2021

I am very new to programming, and previously I made a basic calculator and it uses code like:

def calculator():
    
    num1 = float(input("Enter first number: "))
    oprt = input("Enter an operator(exam: +, -, *, /): ")
    num2 = float(input("Enter second number: "))
    
    if oprt == "+":
        print(num1 + num2)
    elif oprt == "-":
        print(num1 - num2)
    elif oprt == "*":
        print(num1 * num2)
    elif oprt == "/":
        print(num1 / num2)
    else:
        print("Invalid operator")
it works but when I make such a simple choice game with a code like:

def choice_game():

    user = input("Enter your name")
    
    if user == "Daffa":
        print("Hello Daffa")
it won't work, can anyone tell me why the code doesn't work, whatever I wrote in the code, the code still doesn't work, I've also tried using a try statement


RE: Why can't i use def in python? - deanhystad - Jan-15-2021

Please use python tags to wrap posted code so it does not lose the indenting.

Your function works fine for me.
def choice_game():

    user = input("Enter your name")

    if user == "Daffa":
        print("Hello Daffa")

choice_game()
Output:
Enter your nameDaffa Hello Daffa



RE: Why can't i use def in python? - buran - Jan-15-2021

(Jan-15-2021, 02:42 PM)celinafregoso99 Wrote: it won't work, can anyone tell me why the code doesn't work, whatever I wrote in the code, the code still doesn't work, I've also tried using a try statement
what does it mean "doesn't work"? do you get error message?


RE: Why can't i use def in python? - virginiaturner - Apr-10-2021

(Jan-15-2021, 02:42 PM)celinafregoso99 Wrote: I am very new to programming, and previously I made a basic calculator and it uses code like:

def calculator():
    
    num1 = float(input("Enter first number: "))
    oprt = input("Enter an operator(exam: +, -, *, /): ")
    num2 = float(input("Enter second number: "))
    
    if oprt == "+":
        print(num1 + num2)
    elif oprt == "-":
        print(num1 - num2)
    elif oprt == "*":
        print(num1 * num2)
    elif oprt == "/":
        print(num1 / num2)
    else:
        print("Invalid operator")
it works but when I make such a simple choice game with a code like:

def choice_game():

    user = input("Enter your name")
    
    if user == "Daffa":
        print("Hello Daffa")
it won't sports, or score. can anyone tell me why the code doesn't work, whatever I wrote in the code, the code still doesn't work, I've also tried using a try statement

Do you see any error while executing the code? If yes, share a screenshot of the error. It would help to resolve your error.


RE: Why can't i use def in python? - jefsummers - Apr-10-2021

Actually we prefer the poster to use the output tags rather than a screenshot. But yes, posting the error definitely helps