Compare commits
2 Commits
cb66a2bd75
...
7bbe739561
Author | SHA1 | Date |
---|---|---|
perro tuerto | 7bbe739561 | |
perro tuerto | bf2ce70fba |
|
@ -1,3 +1,11 @@
|
|||
"""
|
||||
# hello.py
|
||||
|
||||
The window that appears should contain a cursor awaiting your input,
|
||||
but it’s different from the interactive shell, which runs Python instructions
|
||||
Python Basics as soon as you press enter.
|
||||
"""
|
||||
|
||||
print('Nombre:')
|
||||
nombre = input()
|
||||
print(f"Tu nombre es '{nombre}'.")
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
"""
|
||||
# collatz()
|
||||
|
||||
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.
|
||||
(Amazingly enough, this sequence actually works for any integer—sooner or
|
||||
later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure
|
||||
why. Your program is exploring what’s called the Collatz sequence, sometimes
|
||||
called “the simplest impossible math problem.”)
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
|
||||
def collatz(number):
|
||||
if number % 2 == 0:
|
||||
res = int(number / 2)
|
||||
else:
|
||||
res = 3 * number + 1
|
||||
print(res)
|
||||
return res
|
||||
|
||||
|
||||
def cli():
|
||||
print("Ingresa un número entero:", end=" ")
|
||||
number = input()
|
||||
try:
|
||||
number = int(number)
|
||||
wait = 0.1 / len(str(number))
|
||||
while number != 1:
|
||||
number = collatz(number)
|
||||
time.sleep(wait)
|
||||
except ValueError:
|
||||
print(f"¡'{number}' no es un número entero!")
|
||||
cli()
|
||||
|
||||
|
||||
cli()
|
Loading…
Reference in New Issue