Apr-18-2019, 06:50 AM
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:
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:
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.