繁体   English   中英

如何从python中的字符串中减去整数?

[英]How to have integer subtract from a string in python?

我目前正在从头开始开发一个聊天机器人,作为我的 Python 课程介绍的作业。 在我的作业中,我需要至少有一个“数学组件”。 我似乎无法找出如何从字符串中减去我的输入整数。

附件是一个屏幕截图,我的目标是让他们输入每周在家做饭的天数,然后自动从 7 中减去。

print('Hello! What is your name? ')
my_name = input ()
print('Nice to meet you ' + my_name)
print('So, ' + my_name + ' What is your favorite veggie?')
favorite_veggie = input ()
print('Thats nuts! ' +favorite_veggie + ' is mine too!')
print('How many days a week do you have cook at home? ')
day = input ()
print('So what do you do the other ' + ????? 'days?')
day = int(input())
print('So what do you do the other', 7-day, 'days?')

你正在寻找这个

day = input()
required_days = 7 - int(day)
print('So what do you do the other ' + str(required_days) + ' days?')

您可以将input的结果转换为int ,然后进行减法。 input("")接受一个字符串作为参数,它会显示在提示区的前面,这样可以更好地编写代码

my_name = input('Hello! What is your name? ')
print('Nice to meet you ' + my_name)

favorite_veggie = input('So, ' + my_name + ' What is your favorite veggie?')
print('Thats nuts! ' + favorite_veggie + ' is mine too!')

day = int(input('How many days a week do you have cook at home? '))
non_work_day = 7 - day
what_do = input('So what do you do the other ' + str(non_work_day) + 'days?')

暂无
暂无

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

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