在从主函数调用子函数时,可以将变量转换为int,然后调用。请参考以下代码:import sys

print("Welcome to Calculator\n")

print("Please find the options:\n" + "1. Addition\n" + "2. Subtraction\n" +

"3. Multiplication\n" + "4. Division\n" + "5. Exponential\n" + "6. Quit\n")

def calculator():

choice = input("Enter choice\n")

if int(choice) == 1:

a = input("Enter first number\n")

b = input("Enter second number\n")

add(int(a), int(b))

if int(choice) == 2:

a = input("Enter first number\n")

b = input("Enter second number\n")

diff(int(a), int(b))

if int(choice) == 3:

a = input("Enter first number\n")

b = input("Enter second number\n")

mult(int(a), int(b))

if int(choice) == 4:

a = input("Enter first number\n")

b = input("Enter second number\n")

div(float(a), float(b))

if int(choice) == 5:

a = input("Enter the base number\n")

b = input("Enter the exponential\n")

exp(int(a), int(b))

if int(choice) == 6:

print("Bye")

sys.exit(0)

def add(a, b):

c = a+b

print("Sum of {} and {} is {}".format(a, b, c))

def diff(a,b):

c = a-b

print("Difference between {} and {} is {}".format(a, b, c))

def mult(a, b):

c = a*b

print("The Product of {} and {} is {}".format(a, b, c))

def div(a, b):

c = a/b

print("The Quotient of {} and {} is {}".format(a, b, c))

def exp(a, b):

c = a**b

print("The result of {} to the power of {} is {}".format(a, b, c))

calculator()

这里我做的是在将输入的参数转换为int时调用每个函数,希望这对我有所帮助。

在您的情况下,可以这样更改:if choice == "0":

numberA = raw_input("Enter your first number: ")

numberB = raw_input("Enter your second number: ")

print "Your result is:"

print addition(int(numberA), int(numberB))

python怎么将字符串变成int_在Python中,如何将字符串转换为int?相关推荐

  1. python字符串find函数实现_python中实现查找字符串的find函数

    原博文 2018-06-19 19:26 − 第五题:自己实现一个字符串的find函数1.在一个字符串中查找另一个字符串2.找到了返回第一次出现的位置3.没找到返回-14.参数s1为源字符串,参数s2 ...

  2. scala字符串转int_如何在Scala中将十六进制字符串转换为int?

    scala字符串转int The hex string is also known as Hexadecimal Number String is a number created using hex ...

  3. java中转json字符串_如何在Java中转义JSON字符串-Eclipse IDE技巧

    java中转json字符串 在Java应用程序中工作或进行JSON解析时,通常很常见的做法是从某些资源(例如RESTful Web服务)中复制粘贴JSON字符串,然后使用Jackson库解析JSON. ...

  4. c语言 字符串字符反向储存_C ++中的反向字符串

    c语言 字符串字符反向储存 In many situations, we may need to reverse a string in C++ programming. It may include ...

  5. oracle 截取字符串中间_oracle截取字段中的部分字符串

    使用Oracle中Instr()和substr()函数: 在Oracle中可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符. 其语法为: instr(sourceString,de ...

  6. java文件中查找字符串_Java 在本地文件中查找固定字符串

    适用范围:只适用于在文本文档中查找(如,txt.java.c等等,并不适用与doc.xls等等这些文件),可嵌套文件夹.但是对中文不支持. 例如:文件夹:F:/demo 子文件夹:F:/demo/er ...

  7. python原生字符串可以参与比较_正则表达式中对于原生字符串的理解

    在正则表达式中,有些字符是有特殊意义的字符.因此如果想要匹配这些字符,那么就必须使用反斜杠进行转义.比如$代表的是以...结尾,如果想要匹配$,那么就必须使用\$.示例代码如下: text = &qu ...

  8. python中字符串格式化的形式_Python中format函数字符串格式化入门

    格式化在程序开发中非常常见,大家肯定不陌生,Python中也存在多重格式化方式,format函数就是其中一种. 函数原型format(value[, format_spec]) 参数意义 value: ...

  9. python如何判断列表是否为空_Python中如何检查字符串/列表是否为空

    本文最后更新于2018年5月5日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢! =Start= 缘由: 整理.记录.备忘 正文: 参考解答: 从dict中取值时,一定要使用.get( ...

最新文章

  1. 我在React Native中构建时获得的经验教训
  2. 日志分析logstash插件-grok详解
  3. 10-python-字典
  4. linux源码安装必备条件,玩转Linux必备的金钥匙之源码安装mysql
  5. nvidia显卡对比分析
  6. 你可能没有听说过 js中的 DOM操作还有这个: HTMLCollection 和 NodeList
  7. 戏说 .NET GDI+系列学习教程(三、Graphics类的方法的总结)
  8. 1.5 编程基础之循环控制 35 求出e的值
  9. ZooKeeper(四) 使用Redis RedissonLock 实现分布式锁
  10. 如何快速写一款小而美的“上滑无限加载的控件”?| 博文精选
  11. Java练手小游戏集结,你还在等什么
  12. 软件测试思想者(Software Testing - Thinker) - Logo一览
  13. 系统调用ptrace和进程跟踪
  14. Python应用之批量打水印
  15. 计算机键盘键盘一个按键坏了,我的世界:如果键盘坏掉一个键,六种情况,哪个会影响你玩MC?...
  16. 2019纪中集训总结
  17. 记录一下所有平台emoji表情查询网站
  18. C# 表达式与运算符
  19. python简单小动画
  20. 产品经理必看电影:操作系统革命 Revolution OS

热门文章

  1. 基于SaaS软件即服务模式的报表系统
  2. Flink 远程调试
  3. python竞赛内存_Python的内存机制浅析
  4. 前缀表达式和后缀表达式 - C++代码
  5. 工作和生活之斗转星移
  6. vue 获取微信定位经纬度,并调用高德地图解析出详细地址
  7. 什么是GC Root
  8. file java mkdirs_Java File mkdirs()方法
  9. 手动清理window的右键菜单和win10启动项
  10. Windows本地服务器搭建(微信公众号开发)