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


Messages In This Thread
Python Code doesn't work! - by the_entrepreneur - Apr-18-2019, 06:50 AM
RE: Python Code doesn't work! - by buran - Apr-18-2019, 07:14 AM
RE: Python Code doesn't work! - by the_entrepreneur - Apr-20-2019, 02:25 AM
RE: Python Code doesn't work! - by SheeppOSU - Apr-20-2019, 04:24 AM
RE: Python Code doesn't work! - by the_entrepreneur - Apr-20-2019, 07:14 AM
RE: Python Code doesn't work! - by SheeppOSU - Apr-20-2019, 08:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I can't for the life of me get this basic If statement code to work CandleType1a 8 448 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 349 May-09-2024, 05:39 PM
Last Post: Pedroski55
  hi need help to make this code work correctly atulkul1985 5 981 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 821 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,073 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 829 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 951 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,062 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,620 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 1,015 Dec-29-2022, 08:59 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