-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment2Task3.py
More file actions
67 lines (58 loc) · 2.02 KB
/
assignment2Task3.py
File metadata and controls
67 lines (58 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 15 15:58:03 2020
@author: Jfitz
Assignment 2 - Task 3
Task 3
Write a function named reducer() that accepts an integer parameter named number. If number
is even then reducer() should divide number by 2 and return this value. If number is odd, then
reducer should return 3 times number + 1.
Then write a program that requires a user to enter an integer number and that keeps calling
reducer() on that number until the function returns the value 1. (Amazingly, this sequence
works for any integer value. Sooner or later you will arrive at value 1). Example output
sequence for entering the number 3 is:
10
5
16
8
4
2
1
"""
import sys # used to exit the program
import time #used for holding up the program
def reducer(num):
#we only want to stop when number is 1
while num != 1:
#divide number by 2 and check for no remainder
if num%2 == 0:
#no remainder divide number by 2 and create a new number
num = num/2
#print new number
print(int(num))
else:
#if there is a remainder multiply by 3 and add 1
num = (num*3)+1
#print the result and start again
print(int(num))
def getNumber():
print("************Task 3 : The Reducer **********************")
print("")
#ask user do they want to create a list and declare list name
nList = input("Play the game? [Y/N] ")
#if they want to create a list
if nList == "Y" or nList == "y":
#ask user to enter a number
#if they do not enter a number program will fail with a message
try:
num = int(input("Enter a number: "))
except:
print('not a valid entry - numbers only')
#call the function to perform the task passing the number
reducer(num)
else:
#if user doesnt want to play they see this message
print("Goodbye, and thanks for paricipating")
time.sleep(3)
sys.exit()
getNumber()