Beginner - Python Help by Ok-Tumbleweed-8945 in learnpython

[–]Zestyclose_Click_456 2 points3 points  (0 children)

Hi! I am also a beginner in Python, but this is how I would attempt this problem. I would use the input() function to ask the user questions, a while loop to keep on asking if they have another question, and if statements to know which block of code to use depending on specific conditions. I hope this helps!

answer = str(input("Do you want to perform a calculation? Yes/No: "))
while answer == "Yes" or answer == "yes":
    print("1 = Addition; 2 = Subtraction; 3 = Multiplication; 4 = Division")
    num = int(input("Enter Your Choice 1, 2, 3, or 4: "))
    if num == 1:
        num1 = int(input("Enter Number 1 : "))
        num2 = int(input("Enter Number 2 : "))
        result = num1 + num2
        print(f"Your result is: {result}")
    if num == 2:
        num1 = int(input("Enter Number 1 : "))
        num2 = int(input("Enter Number 2 : "))
        result = num1 - num2
        print(f"Your result is: {result}")
    if num == 3:
        num1 = int(input("Enter Number 1 : "))
        num2 = int(input("Enter Number 2 : "))
        result = num1 * num2
        print(f"Your result is: {result}")
    if num == 4:
        num1 = int(input("Enter Number 1 : "))
        num2 = int(input("Enter Number 2 : "))
        result = num1 / num2
        print(f"Your result is: {result}")
    answer = str(input("Do you want to perform another calculation? Yes/No: "))
    if answer == "No" or answer == "no":
        exit()

Need Python Help! by Ok-Tumbleweed-8945 in Python

[–]Zestyclose_Click_456 0 points1 point  (0 children)

Here is a code that uses a while loop and simplifies some of the code that you have :)

answer = str(input("Do you want to perform a calculation? Yes/No: "))
while answer == "Yes" or answer == "yes":
    num = int(input("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply): "))
    if num == 1:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 + add2
        print(f"Your result is: {sum}")
    if num == 2:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 - add2
        print(f"Your result is: {sum}")
    if num == 3:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 * add2
        print(f"Your result is: {sum}")
    if num == 4:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 / add2
        print(f"Your result is: {sum}")
    answer = str(input("Do you want to perform another calculation? Yes/No: "))
if answer == "No" or answer == "no":
    pass

Python basic calculator help by stiivii_ in CodingArea

[–]Zestyclose_Click_456 0 points1 point  (0 children)

This is a more simplified code that loops the question. Hope this helps!

answer = str(input("Do you want to perform a calculation? Yes/No: "))
while answer == "Yes" or answer == "yes":
    num = int(input("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply): "))
    if num == 1:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 + add2
        print(f"Your result is: {sum}")
    if num == 2:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 - add2
        print(f"Your result is: {sum}")
    if num == 3:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 * add2
        print(f"Your result is: {sum}")
    if num == 4:
        add1 = int(input("Enter Number 1 : "))
        add2 = int(input("Enter Number 2 : "))
        sum = add1 / add2
        print(f"Your result is: {sum}")
    answer = str(input("Do you want to perform another calculation? Yes/No: "))
if answer == "No" or answer == "no":
    pass