我是使用Python进行编程的新手(以及一般的编码),并且想要一些关于小型" helloworld"的帮助。项目
这是我目前的进展:
#meet and greet program in python
print('Hello, nice to meet you')
username = input('Please enter your name: ')
print('Nice to meet you', username)
byear = input('What year were you born? :')
print('So you were born in the year', byear, '?' )
information = input('Is this information is correct? :')
if information == ('Yes') :
print('Ok')
currentyr = input('Please enter the current year:')
print('So that means you are', currentyr - byear,'years old. Right?')
我遇到的麻烦是在最后一行,我希望通过从当前年份减去用户的出生年份(按输入)来查找用户的当前大致年龄(也可以通过输入 - 不确定如何导入当前年份的详细信息。)
这可以在最后一行的中间找到:
,currentyr - byear,
如果有人可以请我解释如何从其他输入变量(byear)中减去此输入变量(currentyr)以达到用户的大致年龄。
提前致谢。
答案 0 :(得分:0)
使用转化为int
:
print('So that means you are', int(currentyr) - int(byear),'years old. Right?')
使用input()
从命令行获取变量时,您将获得字符串。您不能将值减去字符串。 int()
从字符串中生成整数。
答案 1 :(得分:0)
你不能在两个字符串中进行减法。您必须显式地将变量强制转换为整数。使用此表单2017.01.27 10:36:42 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonar/temp
2017.01.27 10:36:42 INFO app[][o.s.p.m.JavaProcessLauncher] Launch process[es]: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121 0.b13.29.amzn1.x86_64/jre/bin/java -Djava.awt.headless=true -Xmx1G -Xms256m -Xss256k -Djna.nosys=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Djava.io.tmpdir=/opt/sonar/temp -javaagent:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-0.b13.29.amzn1.x86_64/jre/lib/management-agent.jar -cp ./lib/common/*:./lib/search/* org.sonar.search.SearchServer /opt/sonar/temp/sq-process7514074371677370036properties
2017.01.27 10:36:51 INFO app[][o.s.p.m.Monitor] Process[es] is up
2017.01.27 10:36:51 INFO app[][o.s.p.m.JavaProcessLauncher] Launch process[web]: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-0.b13.29.amzn1.x86_64/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djruby.management.enabled=false -Djruby.compile.invokedynamic=false -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Djava.io.tmpdir=/opt/sonar/temp -javaagent:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-0.b13.29.amzn1.x86_64/jre/lib/management-agent.jar -cp ./lib/common/*:./lib/server/*:/opt/sonar/lib/jdbc/postgresql/postgresql-9.4.1209.jre7.jar org.sonar.server.app.WebServer /opt/sonar/temp/sq-process6553321235832962376properties
2017.01.27 10:36:54 INFO app[][o.s.p.m.Monitor] Process[es] is stopping
2017.01.27 10:36:54 ERROR app[][o.s.p.m.Monitor] Process[web] failed to start
2017.01.27 10:36:54 INFO app[][o.s.p.m.Monitor] Process[es] is stopped
<-- Wrapper Stopped
将字符串转换为整数。
此代码可以使用。
int(variable)
答案 2 :(得分:0)
#meet and greet program in python
print('Hello, nice to meet you')
username = input('Please enter your name: ')
print('Nice to meet you', username)
byear = int(input('What year were you born? :'))
print('So you were born in the year', byear, '?' )
information = input('Is this information is correct? :')
if information == ('Yes') :
print('Ok')
currentyr = int(input('Please enter the current year:'))
print('So that means you are', currentyr - byear,'years old. Right?')
是Python标准内置函数,用于将字符串转换为整数值。您可以使用包含数字作为参数的字符串来调用它,并返回转换为实际整数的数字:
int()
以上打印2。
这是一个用于计算年龄的简单代码:
print (int("1")+1)
答案 3 :(得分:0)
int():此函数将任何数据类型转换为整数。 因此,在本示例中,我们将首先使用字符串输入,然后使用int()函数将其转换为数字。
num1 = input("Please enter first Number:")
num2 = input("Please enter second Number:")
print(f'Output After Subtraction: {int(num2) - int(num1)}')