I am writing a Collatz sequence program using the practice projects from chapter 3 of Automate the boring stuff with python.
The program outline is:
Write a function named collatz() that has one parameter named number.
If number is even, then collatz() should print number // 2 and return
this value. If number is odd, then collatz() should print and return
3 * number + 1.
Then write a program that lets the user type in an integer and that
keeps calling collatz() on that number until the function returns the
value 1.
My code runs however it stops on 4 rather than 1. For every number I have tried so far the output goes past 1 back to 4.
example output:
6,3,10,5,16,8,4,2,1,4
I am using python 3.4.2
def collatz(number):
if number % 2 == 0:
number = number //2
print(number)
return number
elif number % 2 == 1:
number = 3 * number + 1
print(number)
return number
print ("pick a number:")
while True:
try:
number = int(input())
while number != 1:
number = collatz(number)
collatz(number)
break
except ValueError:
print("Error: Please enter a valid integer")
print("Magic! You are down to 1.")
The problem is that you call collatz() once more after the loop finishes with 1. Just remove that line, and it works fine.
Also if you move the "pick a number" to the input function, you can avoid the new line after the question and are asked again every time, if you input an invalid value.
Additionally you should also check if the number is greater than or equal to 1, to avoid endless loops. The code to do all that would look like that:
while True:
try:
number = int(input("pick a number: "))
if number < 1:
print("Error: Please enter a integer greater than or equal to 1 ")
continue
while number != 1:
number = collatz(number)
# removed the additional call to collatz
break
except ValueError:
print("Error: Please enter a valid integer")
print("Magic! You are down to 1.")
def collatz(number):
number = number // 2 if number % 2 == 0 else 3 * number + 1
print(number)
return number
number = int(input("Pick a Number\n"))
while number != 1:
number = collatz(number)
print("Magic! You are down to 1.")
Related
We want to create a program that prompts the user to enter a number between 1 and 10. As long as the number is out of range the program reprompts the user for a valid number. Complete the following steps to write this code.
a.Write a line of code the prompts the user for number between 1 and 10.
number = float(input("Enter a number between 1 and 10: "))
b. Write a Boolean expression that tests the number the user entered by the code in step "a." to determine if it is not in range.
x = (number > 10 or number < 1)
c.Use the Boolean expression created in step b to write a while loopthat executes when the user input is out of range. The body of the loop should tell the user that they enteredan invalid number and prompt them for a valid number again.
while x == True:
print("you printed an invalid number")
number = float(input("please enter the number again, this time between 1 and 10"))
d.Write the code that prints a message telling the user that they entered a valid number.
if x == False:
print("wow, you printed a number between 1 and 10!")
I answered the stuff for the question, but my problem is that whenever the user enters a wrong number on their first try and a correct number on their second try, the program still considers it as an invalid input. How do I fix this???
Rewrite this line in the while loop:
x = (number > 10 or number < 1)
so it becomes
while x == True:
print("you printed an invalid number")
number = float(input("please enter the number again, this time between 1 and 10"))
x = (number > 10 or number < 1)
This changes the value of x so it doesn't stay at True
If you use a while True construct, you won't need to repeat any code. Something like this:
LO, HI = 1, 10
while True:
input_ = input(f'Enter a number between {LO} and {HI}: ')
try:
x = float(input_)
if LO <= x <= HI:
print(f'Wow! You entered a number between {LO} and {HI}')
break
print(f'{input_} is not in range. Try again')
except ValueError:
print(f'{input_} is not a valid number. Try again')
Note:
When asking for numeric input from the user, don't assume that their input can always be converted properly. Always check
The following code snippet should do all you need:
number = float(input("Please input a number: "))
while (number > 10 or number < 0):
number = float(input("Error. Please input a new number: "))
Use an infinite loop, so that you can prompt for the input only once.
Use break to terminate the loop after the number in the correct range is entered.
Use f-strings or formatted string literals to print the message.
while True:
num = float(input('Enter a number between 1 and 10: '))
if 1 <= num <= 10:
print('Wow, you printed a number between 1 and 10!')
break
else:
print(f'You printed an invalid number: {num}!')
I want to check the Even or Odd numbers therefore I have written the following code:
number = int(input("Which number do you want to check? "))
if number % 2 == 0:
print("This is an even number")
else:
print("This is an odd number")
It gives the following result:
Which number do you want to check? 20
This is an even number
But when is change the above code inversely like this
number = int(input("Which number do you want to check? "))
if number % 2 == 0:
print("This is an odd number")
else:
print("This is an even number")
It gives the following result:
Which number do you want to check? 20
This is an odd number
even if I change the "If modulo operation" to % 7 == 2 it would still give even as odd or vice versa
if number % 2 == 0: print("This is an even number")
else: print("This is an odd number")
But when is change the above code inversely like this
if number % 2 == 0: print("This is an odd number")
else: print("This is an even number")
You only switched the print statements but you didn't update the if condition.
We know that if an integer mod 2 is 0, then it is even... or if an integer mod 2 is 1, then it is odd. So you should change the condition of the bottom piece of code to if number % 2 != 0
question:
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore it.
Input:
7 ,2 , bob, 10, 4, done.
Desired output:
Invalid input
Maximum is 10
Minimum is 2
Actual output:
Invalid input
Invalid input
Maximum is 10
Minimum is 2
Code:
largest=-1
smallest=None
while True:
num =input("Enter a number: ")
try:
if num == "done" :
break
elif smallest is None:
smallest=int(num)
elif int(num)<smallest:
smallest=int(num)
elif int(num)>largest:
largest=int(num)
else:
raise ValueError()
except ValueError:
print("Invalid input")
print("Maximum is",largest)
print("Minimum is",smallest)
I think there's a more Pythonic way of doing this. Try this:
inputList = []
while True:
num = input("Enter a number:")
try:
num = int(num)
inputList.append(num)
except:
if num == "done":
break
else:
print ("Invalid input. Ignoring...")
print ("Maximum is:",max(inputList))
print ("Minimum is:",min(inputList))
Edit: This code works with Python3. For Python2, you might want to use raw_input() instead of input()
You are already capturing the ValueError in Exception,
So inside, try, you are raising ValueError there you leave the scope for this error.
When you accept input, and it accepts 4 as input, which is neither larger than largest (i.e. 10) nor smaller than the smallest (i.e. 2). So it lands in else part and raises ValueError (as per your code). Hence prints Invalid input twice in your case. So this part is unnecessary as well as makes your solution bogus.
Again, from efficiency point of view -
1 - You are checking smallest == None for every input, which takes O(1) time and is unnecessary if you take it any integer
Here is the solution you are looking for :-
largest=None
smallest=None
while True:
try:
num = input("Enter a number: ")
num = int(num)
if smallest is None:
smallest = num
if largest is None:
largest = num
if num < smallest:
smallest = num
elif num > largest:
largest = num
except ValueError:
if num == 'done':
break
else:
print("Invalid input")
continue
print("Maximum is",largest)
print("Minimum is",smallest)
I'm currently working on the collatz project in chapter 3 of Automate the Boring Stuff. I have a fully working collatz function for integer inputs, however I'm stuck trying to get the program to run when I add try, except statements for non-integer values.
Here is my code that only works for integer inputs:
def collatz(number):
if number % 2 == 0:
print(number // 2)
return(number // 2)
else:
print(3 * number + 1)
return(3 * number + 1)
print('Type in a number: ')
colNum = int(input())
while colNum != 1:
colNum = collatz(colNum)
Now, heres my code when I add a try/except statement:
def collatz(number):
if number % 2 == 0:
print(number // 2)
return(number // 2)
else:
print(3 * number + 1)
return(3 * number + 1)
def integercheck(inputVal):
try:
return int(inputVal)
except ValueError:
print('Error: Input needs to be a number.')
print('Type in a number: ')
integercheck(input())
print('Type in a number: ')
colNum = integercheck(input())
while colNum != 1:
colNum = collatz(colNum)
and here is the error code I recieve:
Type in a number:
string
Error: Input needs to be a number.
Type in a number:
string
Error: Input needs to be a number.
Type in a number:
5
Traceback (most recent call last):
File "/Users/Library/Preferences/PyCharmCE2018.2/scratches/scratch_1.py", line 22, in <module>
colNum = collatz(colNum)
File "/Users/Library/Preferences/PyCharmCE2018.2/scratches/scratch_1.py", line 3, in collatz
if number % 2 == 0:
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
Process finished with exit code 1
To be clear, this program works when I throw in an integer immediately, but it fails to work when I throw in an integer after I've typed in a string. If anyone could help I would appreciate it. Thank you!
The recursive case doesn't return anything, so control reaches the end of the function as the call stack resolves (Python functions implicitly return None, hence your error). Change the line:
integercheck(input())
to
return integercheck(input())
or avoid recursion entirely. A few additional remarks:
Avoid printing inside your function. This side effect makes it less reusable.
input("") takes a prompt string that's worth taking advantage of.
Use snake_case in Python per PEP8
collatz only does one step of the sequence, so the name is a little misleading. The burden of setting the next iteration falls on the caller, but this does give the caller some flexibility in determining what to do with intermediate values. With that in mind, this function seems like a perfect fit for refactoring to a generator, giving the ability to take elements one at a time or run the whole sequence as the calling code sees fit.
A possible rewrite:
def collatz(n):
while n > 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
yield n
def get_int(prompt="Enter a number: ",
err="Error: Input needs to be a number."):
while True:
try:
return int(input(prompt))
except ValueError:
print(err)
if __name__ == "__main__":
for step in collatz(get_int()):
print(step)
Seeing as this book is a beginners guide to Python, and I am n00basF the code I wrote that got this to work is as follows:
def collatz(number):
try:
number = int(number)
while (number) != 1:
if number % 2 == 0:
number = number // 2
print(number)
elif number % 2 == 1:
number = 3 * number + 1
print(number)
else:
break
except ValueError:
print('ERROR!!! You need to enter an integer.')
print('Enter a number: ')
userNumber = input('>> ')
collatz(userNumber)
My code is below. To do the exception handling, I put it in a while loop that breaks to call the collatz function.
In my collatz function, it tests to see if the number is 1 first, then proceeds to check if the number is even or odd, and proceeds with the operation to create the new number and prints it. I call the collatz function recursively within my odd / even blocks using my new number.
def collatz(number):
if int(number) == 1:
print('')
elif int(number) % 2 == 0:
newNumber = int(number / 2)
print(newNumber)
collatz(newNumber)
elif int(number) % 2 == 1:
newNumber = int(3 * number + 1)
print(newNumber)
collatz(newNumber)
print('Enter a number:')
while True:
try:
number = int(input())
break
except ValueError:
print('Please enter a NUMBER:')
collatz(number)
This question already has an answer here:
Python Checking 4 digits
(1 answer)
Closed 1 year ago.
I want to write a program that only accepts a 4-digit input from the user.
The problem is that I want the program to accept a number like 0007 but not a number like 7 (because it´s not a 4 digit number).
How can I solve this? This is the code that I´ve wrote so far:
while True:
try:
number = int(input("type in a number with four digits: "))
except ValueError:
print("sorry, i did not understand that! ")
if number > 9999:
print("The number is to big")
elif number < 0:
print("No negative numbers please!")
else:
break
print("Good! The number you wrote was", number)
But if I input 7 to it it will just say Good! The number you wrote was 7
Before casting the user's input into an integer, you can check to see if their input has 4 digits in it by using the len function:
len("1234") # returns 4
However, when using the int function, Python turns "0007" into simple 7. To fix this, you could store their number in a list where each list element is a digit.
If it's just a matter of formatting for print purposes, modify your print statement:
print("Good! The number you wrote was {:04d}", number)
If you actually want to store the leading zeros, treat the number like a string. This is probably not the most elegant solution but it should point you in the right direction:
while True:
try:
number = int(input("Type in a number with four digits: "))
except ValueError:
print("sorry, i did not understand that! ")
if number > 9999:
print("The number is to big")
elif number < 0:
print("No negative numbers please!")
else:
break
# determine number of leading zeros
length = len(str(number))
zeros = 0
if length == 1:
zeros = 3
elif length == 2:
zeros = 2
elif length == 3:
zeros = 1
# add leading zeros to final number
final_number = ""
for i in range(zeros):
final_number += '0'
# add user-provided number to end of string
final_number += str(number)
print("Good! The number you wrote was", final_number)
pin = input("Please enter a 4 digit code!")
if pin.isdigit() and len(pin) == 4:
print("You successfully logged in!")
else:
print("Access denied! Please enter a 4 digit number!")