Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Code doesn't work!
#1
I've started learning Python for a few days now. Basically, I do not understand a lot of terms these Python developers use and I dont really know how to fix my code.

I was learning Python from this: https://www.youtube.com/watch?v=rfscVS0vtbw&t=9301s

There was a part where we had to create a very simple calculator. In the video, the code seemed to work perfectly. But it didn't work as well for me. I use PyCharm, by the way. It doesn't show any errors, but the code doesn't serve its purpose

Here's the code for the simple calculator:


# Program make a simple calculator that can add, subtract, multiply and divide using functions

# This function adds two numbers
def add(num1, num2):
   return num1 + num2

# This function subtracts two numbers
def subtract(x, y):
   return x - y

# This function multiplies two numbers
def multiply(x, y):
   return x * y

# This function divides two numbers
def divide(x, y):
   return x / y



print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user
choice = input("Enter choice(1/2/3/4):")

num1 = float(input("Enter first number: "))
num2: float(input("Enter second number: "))

if choice == '1':
   print(add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")


Some functions seem to work at times, but most of the time it just doesn't work.

Here's another word guessing game I built which doesn't work either:
secret_word = "harry"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while guess != secret_word and not(out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Enter your guess: ")
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("Out of guesses. Better luck next time!")
else:
    print("Congrats! You win!")
I use Python 3 & I feel this should work. Why doesn't it? I'd really appreciate it if you looked into this ASAP & gave me some feedback.
Reply
#2
first of all I don't think

(Apr-18-2019, 06:50 AM)the_entrepreneur Wrote: It doesn't show any errors

is correct. You should get NameError for num2 being not defined.
Then, to fix it compare lines 30 and 31. Do you see the difference?

(Apr-18-2019, 06:50 AM)the_entrepreneur Wrote: Here's another word guessing game I built which doesn't work either:
Please, start separate thread for this question. And provide more info as to how exactly it does not work
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Oops, I put in a colon instead of an equals to symbol. I did fix that, but that doesn't solve the problem yet. The calculator still doesn't work. Do you think I might have a problem with my version? It says that it needs to update but I've not updated it yet. But I'm pretty sure it's Python 3. Do you think that there might be something wrong with the code for the 'operations'?
Reply
#4
I fixed your word guessing game. Go back to your original thread while I fix this one

Here this should work. I tested it
# Program make a simple calculator that can add, subtract, multiply and divide using functions
 
# This function adds two numbers
def add(num1, num2):
   return num1 + num2
 
# This function subtracts two numbers
def subtract(x, y):
   return x - y
 
# This function multiplies two numbers
def multiply(x, y):
   return x * y
 
# This function divides two numbers
def divide(x, y):
   return x / y
 
 
 
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
 
# Take input from the user
choice = input("Enter choice(1/2/3/4):")
 
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
 
if choice == '1':
   print("%s + %s = %s" %(num1, num2, add(num1, num2)))
 
elif choice == '2':
   print("%s - %s = %s" %(num1, num2, subtract(num1,num2)))
 
elif choice == '3':
   print("%s * %s = %s" %(num1, num2, multiplay(num1, num2)))
 
elif choice == '4':
   print("%s / %s = %s" %(num1, num2, divide(num1, num2)))
else:
   print("Invalid input")
Reply
#5
I tried it. Still doesn't work.
Reply
#6
IDK what's wrong because it worked for me
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 772 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 675 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 929 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 709 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 816 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,774 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,427 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 883 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,231 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,426 Jun-30-2022, 06:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020