在这个python脚本中,为什么它不像它应该的那样做减法?

问题描述 投票:-1回答:1

在论坛上,我只是为了搞笑,看看我学到了多少东西,我遇到了一个问题......下面是我写的,为什么它不减法,因为它的假设,有什么东西我应该考虑?

numbers = input("what number would you like to calculate? ")

print(numbers)
first = numbers[0]
second = numbers[1]
third = numbers[2]
for i in range(1,10):
    z = int(third) + (i * 3)
    if z >= 10:
        z = z - 10
    y = int(second) + (i * 2)
    if y >= 10:
        y = y - 10
    x = int(first) + (i * 1)
    if x >= 10:
        x = x - 10    
    print(x, y, z)
    i += 1

关于这个问题的任何意见都将被感激,谢谢你的时间。

python if-statement subtraction
1个回答
0
投票

对我来说是可行的,虽然我不知道你期望的输出是什么。我注意到的一个问题是,你不允许在输入字段中使用空格。numbers 是一个字符串,你可以使用其中的前三个字符。没有'''的整数版本,所以它会抛出一个错误。

代码的输出是。

what number would you like to calculate? 123
123
2 4 6
3 6 9
4 8 2
5 0 5
6 2 8
7 4 11
8 6 14
9 8 17
0 10 20

对我来说,这似乎是正确的


0
投票

我在乱七八糟的情况下,又在y和z上加了两个if语句,这样就得到了我想要的结果,代码如下。

numbers = input("what number would you like to calculate? ")
print(numbers)
first = numbers[0]
second = numbers[1]
third = numbers[2]
for i in range(1,10):
    z = int(third) + (i * 3)
    if z >= 10:
        z = z - 10
    if z >= 10:
        z = z - 10
    if z >= 10:
        z = z - 10
    y = int(second) + (i * 2)
    if y >= 10:
        y = y - 10
    if y >= 10:
        y = y - 10
    if y >= 10:
        y =y - 10
    x = int(first) + (i * 1)
    if x >= 10:
        x = x - 10    
    print(x, y, z)
    i += 1
© www.soinside.com 2019 - 2024. All rights reserved.