Python新手求助:计算器结果存储及循环逻辑错误问题
Hey there! Let's fix up your calculator and tackle those two issues you mentioned. I'll break down the problems and show you a revised version of your code with explanations.
First, let's address your core issues:
1. Storing the calculation result correctly
In your original code, you assigned num3 four times in a row:
num3 = num1 / num2 num3 = num1 - num2 num3 = num1 + num2 num3 = num1 * num2
This means num3 will only hold the value of num1 * num2 (the last assignment), not the result of the operator the user chose. We need to set num3 only based on the operator the user selected.
2. Fixing the "continue" condition bug
Your line if continue1 == "yes" or "Yes" doesn't work as expected. Python evaluates this as (continue1 == "yes") or ("Yes") — and since non-empty strings are always True in Python, this condition will always be true, even if the user enters "no". The correct way is to check if the input matches either "yes" or "Yes", or better yet, convert the input to lowercase to handle all variations (like "YES" or "yEs"):
if continue1.lower() == "yes":
Revised Calculator Code (with continuous calculation support)
Here's a polished version that stores the result and lets you keep calculating with it:
# Start the calculator with the first number try: current_result = float(input("Please enter your first number: ")) except ValueError: print("Oops, that's not a valid number!") exit() while True: # Get second number for the next calculation try: next_num = float(input("Please enter your next number: ")) except ValueError: print("Invalid number, try again!") continue operator = input("Please enter operator (+, -, *, /): ").strip() # Perform calculation based on operator if operator == "/": if next_num == 0: print("Error: Can't divide by zero!") continue current_result = current_result / next_num elif operator == "+": current_result = current_result + next_num elif operator == "-": current_result = current_result - next_num elif operator == "*": current_result = current_result * next_num else: print("Invalid operator! Please use +, -, *, /") continue # Show the latest result print(f"Current result: {current_result}") # Ask user if they want to continue continue_choice = input("Would you like to continue? [Yes/No]: ").strip().lower() if continue_choice != "yes": print("Thanks for using the calculator!") break input("Press Enter to Exit")
Key improvements in this code:
- Continuous calculation: The result of each operation is stored in
current_result, which becomes the first number for the next calculation automatically. - Input validation: Added
try/exceptblocks to handle cases where the user enters non-numeric values, plus a check to prevent division by zero. - Fixed continue condition: Uses
.strip().lower()to handle extra spaces and any case variation of "yes" (like "YES" or "yEs"). - Cleaner loop structure: Uses a
while Trueloop to keep the calculator running until the user chooses to stop, making the flow more intuitive.
Let me know if you have any questions about how this works! 😊
内容的提问来源于stack exchange,提问作者Musa Ahmed

