Python将一个3位数的数字分成两部分

2024-09-26 22:50:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写基于巧克力数学游戏(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))

我觉得我应该在这里的某个地方有个功能,但我太生疏了,不知道在哪里。在


Tags: 代码youinputyour数字currentyearnum
2条回答

有多种方法可以解决它-一种是使用模数,即533%100=33,或者你可以把它当作一个字符串,选择最后两个数字str(533)[-2:]。在

这就是你想要达到的目标吗?我不确定我真的理解你想在这里完成什么,但是我用这个实现的输出似乎与你描述的一致。在

不过,需要注意的是,你的年龄(以年为单位)有可能会被解雇,因为你没有考虑月数/补偿。在

例如,输入:我想要一周三天的巧克力,我今年还没有生日,出生于96年,结果是:

Okay, tell me how many times a week you want chocolate.
It should be more than one, but less than 10: 3

Have you had your birthday this year? Y/N: N
What year were you born? (Don't worry, I won't look): 1996
Well! Your number is 319. That means your original number was 3, and you are 19 years old

应该是20;)

我假设这就是250和{}的用途,在这种情况下,它们应该被切换?再次,不确定,因为现在他们是魔术数字。在

^{pr2}$

相关问题 更多 >

    热门问题