繁体   English   中英

我正在制作计算器,问题出在第 42 行 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

[英]I am making a calculator the issue is on line 42 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

无法弄清楚第 42 行不断收到错误 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' 有人可以建议我如何修复它,因为我已经坚持了几个小时代码是如何工作的你运行它然后你选择你想要在这种情况下做哪种类型的计算圆圈被打破并且由于某种原因它不会工作它应该如何工作是def check检查字符串是否是数字或字母,如果它们是数字,它会继续,如果是字母,它会重新开始,并说你需要选择数字而不是字母

import time
import math

def check(num1, num2):
    number1 = str(num1).isnumeric()
    if number1 == False:
        print('Bruh math not l\'anglais')
        time.sleep(0.5)
        restart()
    number2 = str(num2).isnumeric()
    if number2 == False:
        print('Bruh math not l\'anglais')
        time.sleep(2.5)
        while True:
            restart()
       
def percentage_decrease():
    original_amount = str(input("Please give me the original amount: "))
    percentage_decrease = str(input("Give me percentage to decrease: "))
    check(original_amount, percentage_decrease)
    multiplier = 1 - int(percentage_decrease) / 100
    answer = int(original_amount) * multiplier
    print("The answer is",answer)                        
def percentage_increase():
    original_amount = str(input("Please give me the original amount: "))
    percentage_increase = str(input("Give me percentage to increase: "))
    check(original_amount, percentage_increase)
    multiplier = 1 + int(percentage_increase) / 100
    answer = int(original_amount) * multiplier
    print("The an is",answer)
def power_of_x():
    num = str(input("What number would you like to find out the power off: "))
    power = str(input("what power do you to do: "))
    check(num, power)
    answer = int(num**power)
    print("The answer is",answer)
def area_circle():
    user_choice = input("Would you like to use diameter or radius?")
    if user_choice.lower() == "radius" or user_choice.lower() == "r":
        num_area_circle_radius = str(input("Please give me the radius: "))
        check(num_area_circle_radius, '0')
        anwser_area_circle = math.pi * int(num_area_circle_radius**2)
        rounded_anwser_area_circle = round(anwser_area_circle, 2)
        print("The anwser is",rounded_anwser_area_circle,"rounded 2d.p")
    elif user_choice.lower() == "diameter" or user_choice.lower() == "d":
        num_area_circle_diameter = str(input("Please give me the diameter: "))
        check(num_area_circle_diameter, '0')
        convert_diameter_radius = num_area_circle_diameter / 2
        anwser_area_circle = math.pi * int(convert_diameter_radius**2)
        rounded_anwser_area_circle = round(anwser_area_circle, 2)
        print("The anwser is",rounded_anwser_area_circle,"rounded 2d.p")                
def addition():
    num1_addition = input("First Number pls: ")
    num2_addition = input("Second Number pls: ")
    check(num1_addition, num2_addition)
    answer_addition = int(num1_addition) + int(num2_addition)
    print("The answer is",answer_addition)
def multiply():
    num1_multiply = str(input("First Number pls: "))
    num2_multiply = str(input("Second Number pls: "))
    check(num1_multiply, num2_multiply)
    answer_multiply = int(num1_multiply) * int(num2_multiply)
    print("The answer is",answer_multiply)  
def subtraction():
    num1_subtraction = str(input("First Number pls: "))
    num2_subtraction = str(input("Second Number pls: "))
    check(num1_subtraction, num2_subtraction)
    answer_subtraction = int(num1_subtraction) - int(num2_subtraction)
    print("The answer is",answer_subtraction)
def division():
    num1_division = str(input("First Number pls: "))
    num2_division = str(input("Second Number pls: "))
    check(num1_division, num2_division)
    answer_division = int(num1_division) / int(num2_division)
    print("The answer is",answer_division)
def start():
    print("These are multiple things you can do \n1)Addition \n2)Subtraction \n3)Multiplication \n4)Division \n5)Area Of Circle \n6)Power Off x \n7)Percentage Increase \n8)Percentage Decrease")
    time.sleep(0.5)
    user_operator = input("What calculation would you like to: ")
    if user_operator.lower() == "Addition" or user_operator == "1":
        addition()
    elif user_operator.lower() == "Subtraction" or user_operator == "2":
        subtraction()
    elif user_operator.lower() == "Division" or user_operator == "4":
        division()
    elif user_operator.lower() == "Multiplication" or user_operator == "3":
        multiply()
    elif user_operator.lower() == "Area of a circle" or user_operator == "5":
        area_circle()
    elif user_operator.lower() == "power of x" or user_operator == "6":
        power_of_x()
    elif user_operator.lower() == "percentage increase" or user_operator == "7":
        percentage_increase()
    elif user_operator.lower() == "percentage decrease" or user_operator == "8":
        percentage_decrease()
    else:
        start()
start()
def restart():
    user_restart = input("Do you want to do another calculation(y or n): ")
    if user_restart == "Y".lower():
        start()
    elif user_restart == "N".lower():
        print("Thank you for using my calculator.")
    else:
        start()        
    while user_restart == "Yes".lower():
        restart()
        if user_restart == "No".lower():
            user_restart = "No"
while True:
    restart()
    num = str(input("What number would you like to find out the power off: "))
    power = str(input("what power do you to do: "))
    check(num, power)
    answer = int(num**power)

您打算将“num”转换为 int,然后取幂,但您没有对齐括号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM