LU06.L12: Split in pieces
1. Einlesen
def main():
print('Give a numbers:')
number = 0
while number != -1:
number = int(input())
print('Thx! Bye!')
if __name__ == '__main__':
main()
2. Summe der Zahlen
def main():
print('Give a numbers:')
number = 1 # To compensate for -1 for terminating
sum = 0
while number != -1:
number = int(input())
sum += number
print('Thx! Bye!')
print(f'Sum: {sum}')
if __name__ == '__main__':
main()
3. Summe und die Anzahl der Zahlen
def main():
print('Give a numbers:')
number = 1 # To compensate for -1 for terminating
sum = 0
counter = -1 # We start with -1 to compensate for last iteration
while number != -1:
number = int(input())
sum += number
counter += 1
print('Thx! Bye!')
print(f'Sum: {sum}')
print(f'Numbers: {counter}')
if __name__ == '__main__':
main()
4. Durchschnitt der Zahlen
def main():
print('Give a numbers:')
number = 1 # To compensate for -1 for terminating
sum = 0
counter = -1 # We start with -1 to compensate for last iteration
while number != -1:
number = int(input())
sum += number
counter += 1
average = sum / counter
print('Thx! Bye!')
print(f'Sum: {sum}')
print(f'Numbers: {counter}')
print(f'Average: {average}')
if __name__ == '__main__':
main()
5. Gerade und ungerade Zahlen
def main():
print('Give a numbers:')
number = 0
sum = 1
counter = -1 # We start with -1 to compensate for last iteration
odd = 0
even = 0
while number != -1:
number = int(input())
if (number % 2 == 0 and number > 0):
even += 1
if (number % 2 == 1 and number > 0):
odd += 1
sum += number
counter += 1
average = sum / counter
print('Thx! Bye!')
print(f'Sum: {sum}')
print(f'Numbers: {counter}')
print(f'Average: {average}')
print(f'Even: {even}')
print(f'Odd: {odd}')
if __name__ == '__main__':
main()