微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基本计算器,多种输入和功能选择

如何解决基本计算器,多种输入和功能选择

正如我的代码可以告诉我的那样,我想做一个基本的计算器,首先要问你要做什么,然后问2输入2的数字由代码计算出来。我遇到了一个问题,代码没有返回任何内容,甚至都没有尝试使用它想要的功能

##Simple Calculator program##
print("Welcome to my basic calculator program")

print("In this program you will be asked to input what function you want to do then")
print("select 2 numbers,where the program will then do the mathematic operation on those 2 numbers")
#Class containing the functions and basic caluclation
 
class calculator_class():
    print("Please select a function by entering these :")
    print("Addition")
    print("Subtraction")
    print("Multiplication")
    print("Division")
    #this is a function which asks the user to choose what operator to choose before choosing their number
    
    def userchoice():
        
        userchoices = str(input())
        if userchoices in ["Addition","Subtraction","Multiplication","Division"]:
            return(
        
        
        if userchoices == "Addition":
            print(addition())
        elif userchoices == "Subtraction":
            print(subtraction())
        elif userchoices == "multiplication":
            print(multiplication())
        elif userchoices == "division":
            print(division())
        else:
            print(invalid_choice())
    print(userchoice())
    #here the user chooses the 2 numbers
    print("Please select 2 numbers to calculate")
        
    usernumber1 = int(input("Please input your first number here  : "))
    usernumber2 = int(input("Please input your second number here : "))
        
    #Functions of which contain addition,subtraction,multiplication and division
    def addition():
        print("A D D I T I O N")
        print("Just calculating...")
        
        print(usernumber1 + usernumber2)
        
    
    def subtraction():
        print("S U B T R A C T I O N")
        print("Just calculating...")
        print(usernumber1 - usernumber2)
        
    
    def multipliction():
        print("M U L T I P L I C A T I O N ")
        print("Just calculating...")
        print(usernumber1 * usernumber2)
        
    
    def division():
        print("D I V I S I O N ")
        print("Just calculatin...")
        print(usernumber1 / usernumber2)
        
        
    def invalid_choice():
        print("You did not pick a valid option,please try again")
    

解决方法

您在此代码中有很多错误的方法。

  1. 键入一个符号(例如*)比输入“乘法”更容易
  2. 最好为每个用户输入应用#include <stdio.h> int main() { int input; long double result = 1; printf("Enter the integer number : "); scanf("%d",&input); printf("n:!\n\n"); for(int i = 1; i <= input; i++) { result *= i; if(i%2==1){ printf("%d:%Le\n",i,result); } //note that im using %Le for long double type }
  3. python中的输入始终为str,因此.lower()中的str()是多余的
  4. userchoices = str(input())的{​​{1}}可能会导致错误(int()),因此请将此类代码放在input()块中

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?