我正在编写基于巧克力数学游戏(http://www.joe-ks.com/Chocolate_Math.htm)的代码。我可以让代码正常运行,除了最后一部分。在
我希望输出显示,例如,“您的最终数字是533。这意味着你原来的号码是5,而你已经33岁了。”
我搞不懂怎么只打印三位数数字的最后两位数。在
orig_num = int(input("Okay, tell me how many times a week you want chocolate. It should be more than one, but less than 10: "))
guess = ((orig_num * 2) + 5) * 50
birthday = input("Have you had your birthday this year? Y/N: ")
if birthday == 'Y':
guessb = guess + current_year - 250
elif birthday == 'N':
guessb = guess + current_year - 251
year = int(input("What year were you born? (Don't worry, I won't look): "))
less_year = guessb - year
print("Well! Your number is {}. That means your original number was {}, and you are {} years old".format(less_year, orig_num, less_year))
我觉得我应该在这里的某个地方有个功能,但我太生疏了,不知道在哪里。在
有多种方法可以解决它-一种是使用模数,即
533%100=33
,或者你可以把它当作一个字符串,选择最后两个数字str(533)[-2:]
。在这就是你想要达到的目标吗?我不确定我真的理解你想在这里完成什么,但是我用这个实现的输出似乎与你描述的一致。在
不过,需要注意的是,你的年龄(以年为单位)有可能会被解雇,因为你没有考虑月数/补偿。在
例如,输入:我想要一周三天的巧克力,我今年还没有生日,出生于96年,结果是:
应该是20;)
我假设这就是}的用途,在这种情况下,它们应该被切换?再次,不确定,因为现在他们是魔术数字。在
^{pr2}$250
和{相关问题 更多 >
编程相关推荐