-5

My task is to make a program.

In the code there is variable called num1, num1 has to be digits only and with the length of 5.

The code should print every digit in num1 and their sum.

The problem here is that if num1's length is not 5 it's not working, as well if there is chars in num1 that aren't digits.

It seems like the problem is in the condition.

Please tell me what is not good in my program (I'm still a beginner in python) EDIT : Thanks for the help! i appreciate it, i got along with it, the new code is the right one. :D

max_value = 5

def main():
    global max_value
    #This line requests from you to put an input with 5 digits
    num1 = raw_input("Please enter a number with 5 digits\n")
    bol = check_everything(num1)
    if bol is False:
        while (len(num1) != max_value) or (num1.isdigit() == False):
            num1 = raw_input("Please enter a number with 5 digits and with 
digits only\n")
    num1 = int(num1)
    printer(num1)


def check_everything(num1):
    if (len(num1) == max_value) and (num1.isdigit() == True):
        return True
    else:
        return False


def printer(num1):
    summ = 0
    s1 = ''
    for x in xrange(max_value):
        s1 += str(num1)[x] + ','
    print s1[:-1:]
    while num1 != 0:
        summ += num1 % 10
        num1 /= 10
    print(str(summ))


if __name__ == '__main__':
    main()
4

3 回答 3

0

您可以使用 a 设置条件while loop,该条件仅接受string满足具有 alen of 5is.digit()

num1 = input('Enter a number with a length of 5: ')
while len(num1) != 5 and num1 != num1.isdigit():
        num1 = input('Please enter a number with a length of 5: ')
num1 = int(num1)
于 2018-10-01T22:07:26.123 回答
0

我的任务是编写代码。在代码中有一个名为 num1 的变量,num1 只能是数字,长度为 5。代码应该打印 num1 中的每个数字,它们是总和。这里的问题是,如果 num1 长度大于 5 或​​小于 5 它不起作用,以及 num1 中有不是数字的字符。

这听起来像家庭作业。

此外,您的代码示例是屏幕截图。请在问题正文的格式化代码块中包含完整的示例代码。

更多细节也是必要的,但我认为以下可能是您开始思考如何完善您的问题的一个很好的起点:

import re

def print_numbers_and_sum(input_string: str):
    """
    Find all numbers in an input string, print them and their sum
    """

    all_numbers = []

    # Using Regular Expressions (re), define a pattern 
    # that will catch numbers from the input string
    number_pattern = r"[-+]?[0-9]*\.?[0-9]*"
    number_regex = re.compile(number_pattern)

    # Iterate over the matches in our input string
    for number_string in number_regex.findall(input_string):

        # the input string can potentially be empty
        if number_string:

            current_number = float(number_string)
            all_numbers.append(current_number)
            print(current_number)

    # Sum is part of the standard library and will sum values in an iterable
    print(sum(all_numbers))

if __name__ == "__main__":
    input_string = "-206.35 and another number 4005.32"
    print_numbers_and_sum(input_string)
于 2018-09-30T21:08:17.520 回答
0

尝试这个:

# -*- coding: utf-8 -*-
max_value = 5


#The main function, it only summons the other functions and gets an input for num1.

#If the value entered in the functions is not ok, a loop happens in the main function.
def main():
    global max_value
    #This line requests from you to put an input with 5 digits
    num1 = input("Please enter a number with 5 digits\n")
    bol = check_everything(num1)
    if bol == False:
        while (len(num1) != max_value) or (num1.isdigit() == False):
            num1 = input("Please enter a number with 5 digits and with digits only\n")
    num1 = int(num1)
    printer(num1)


def check_everything(num1):
    if (len(num1) == max_value) and (num1.isdigit() == True):
        return True
    else:
        return False


def printer(num1):
    numsum = 0
    s1 = ''
    for x in range(max_value):
        s1 += str(num1)[x] + ','
    print(s1[:-1:])
    for i in range(len(str(num1))):
      numsum = numsum + int(str(num1)[i:i+1])
    print(str(numsum))


if __name__ == '__main__':
  main()
于 2018-10-01T15:13:48.770 回答