示例#1
0
def guess():
    i = 0
    # i is the lowest number in range of possible guess
    j = 100
    # j is the highest number in range of possible guesses
    m = 50
    # m is the middle number in range of possible guesses
    counter = 1
    # counter is the number of guesses take.
    print("Please guess a number")
    condition = myFunctions.get_int(
        "Is your guess " + str(m) +
        "? (0 means it's too low, 1 means it's your guess and 2 means it's too high) "
    )
    while condition != 1:
        counter += 1
        if condition == 0:
            i = m + 1
        elif condition == 2:
            j = m - 1
        m = (i + j) // 2
        condition = myFunctions.get_int(
            "Is your guess " + str(m) +
            "? (0 means it's too low, 1 means it's your guess and 2 means it's too high) "
        )
    print("It took", counter, "time(s) to guess your number (", m, ")")
示例#2
0
# Ask the user to guess the number, then tell them whether they guessed too low,
# too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)

# Extras:
# Keep the game going until the user types “exit”
# Keep track of how many guesses the user has taken, and when the game ends, print this out.

import random
import myFunctions

number = random.randint(1, 9)
userResponse = ''
userNumber = 0
keepPlaying = True
guessCounter = 0

while keepPlaying == True:
    userNumber = myFunctions.get_int("Guess the secret number from 1 to 9: ")
    if userNumber == number:
        print("You Win!")
        guessCounter += 1
        break
    elif userNumber < number:
        print("Your guess was too low. Keep trying!")
        guessCounter += 1
    elif userNumber > number:
        print("Your guess was too high. Keep trying!")
        guessCounter += 1

print("Secret number: " + str(number))
print("Number of guesses: " + str(guessCounter))
示例#3
0
# Exercise 4
# Create a program that asks the user for a number and then prints out a list of all the divisors of that number.
# (If you don’t know what a divisor is, it is a number that divides
# evenly into another number. For example, 13 is a divisor of 26 because 26 / 13 has no remainder.)

import myFunctions


def generateDivisorList(number: int):
    divisorList = []
    for i in range(number, 0, -1):
        if number % i == 0:
            divisorList.append(int(number / i))
    print("List of divisors for number " + str(number) + " : " +
          str(divisorList))


generateDivisorList(myFunctions.get_int("Insert number: "))
示例#4
0
# Exercise 11
# Ask the user for a number and determine whether the number is prime or not. 
# (For those who have forgotten, a prime number is a number that has no divisors). 
# You can (and should!) use your answer to Exercise 4 to help you. Take this opportunity 
# to practice using functions, described below.

import myFunctions

num = myFunctions.get_int('Insert a number: ')
if myFunctions.is_prime(num):
    print("The number", num, "is prime")
else:
    print("The number", num, "is NOT prime")
示例#5
0
# Exercise 16
# Write a password generator in Python. Be creative with how you generate passwords -
# strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols.
# The passwords should be random, generating a new password every time the user asks for a new password.
# Include your run-time code in a main method.

# Extra:
# Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list.

import myFunctions

passSize = myFunctions.get_int(
    "How many characters would you like your password to have?: ")

print(myFunctions.pass_generator(passSize))
示例#6
0
# Extras:

# Add on to the previous program by asking the user for another number and printing out that many copies
# of the previous message. (Hint: order of operations exists in Python)
# Print out that many copies of the previous message on separate lines.

import datetime
import myFunctions


def hundred_years_calculator(age) -> int:
    now = datetime.datetime.now()
    currentYear = now.year
    finalDate = currentYear + 100 - age
    return finalDate


def multiple_print(times, date, name):
    while times > 0:
        print("Dear " + name + ", you will turn 100 years old on " + str(date))
        times -= 1


if __name__ == "__main__":

    name = input("Enter your name: ")
    age = int(input("Enter your age: "))
    date = hundred_years_calculator(age)
    copy_message = myFunctions.get_int(
        "How many times you want to print the message: ")
    multiple_print(copy_message, date, name)
示例#7
0
# Exercise 13
# Write a program that asks the user how many Fibonnaci numbers to generate and then generates them.
# Take this opportunity to think about how you can use functions.
# Make sure to ask the user to enter the number of numbers in the sequence to generate.
# (Hint: The Fibonnaci seqence is a sequence of numbers where the next number in the sequence
# is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …)

import myFunctions

userInput = myFunctions.get_int(
    "How many Fibonacci numbers in the sequence?: ")


def generateFibonacciSequence(number: int) -> list:
    i = 1
    if number == 0:
        sequence = []
    elif number == 1:
        sequence = [1]
    elif number == 2:
        sequence = [1, 1]
    elif number > 2:
        sequence = [1, 1]
        while i < (number - 1):
            sequence.append(sequence[i] + sequence[i - 1])
            i += 1
    return sequence


print(generateFibonacciSequence(userInput))
示例#8
0
# Extras:

# If the number is a multiple of 4, print out a different message.
# Ask the user for two numbers: one number to check (call it num) and one number to divide by (check).
# If check divides evenly into num, tell that to the user. If not, print a different appropriate message.

import myFunctions


def even_check(number) -> str:
    if number % 4 == 0:
        result = "EVEN & MULTIPLE OF 4"
    elif number % 2 == 0:
        result = "EVEN"
    else:
        result = "ODD"
    return result


number = myFunctions.get_int("Enter a number: ")
result = even_check(number)
print("Dear user, number " + str(number) + " is " + result)

num = myFunctions.get_int("Enter another number: ")
check = myFunctions.get_int("Enter a number to divide the previous by: ")

if num % check == 0:
    print(num, "divides evenly by", check)
else:
    print(num, "does not divide evenly by", check)
示例#9
0
# Extras:

# Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this
# list in it and print out this new list.
# Ask the user for a number and return a list that contains only elements from the original list a that are
# smaller than that number given by the user.

import myFunctions
import random

numberList = myFunctions.random_number_list()
print(numberList)
lowerThan5List = []
lowerThanInputList = []

inputNumber = myFunctions.get_int("Enter a number: ")


def printLowerThan5(nList: list):
    print("Numbers lower than 5: ")
    for number in nList:
        if number < 5:
            print(number)
            lowerThan5List.append(number)
        if number < inputNumber:
            lowerThanInputList.append(number)


printLowerThan5(numberList)
print("List of numbers lower than 5: " + str(lowerThan5List))
print("List of numbers lower than input: " + str(lowerThanInputList))
示例#10
0
    end_index = len(alist)
    while True:
        middle_index = (end_index + start_index) // 2
        if number == alist[middle_index]:
            return True
        elif middle_index == start_index:
            return False
        elif number < alist[middle_index]:
            end_index = middle_index
        else:
            start_index = middle_index


if __name__ == "__main__":
    orderedList = [1, 2, 5, 9, 16, 22, 24]
    number = myFunctions.get_int("Enter number: ")

    if regularSearch(number, orderedList):
        print("Regular Search: The number " + str(number) +
              " is in the list " + str(orderedList))
    else:
        print("Regular Search: The number " + str(number) +
              " is NOT in the list " + str(orderedList))

# Binary
a = [0, 1, 3, 4, 5, 6, 7, 8, 9]
print(find(a, 0))  # returns True
print(find(a, -1))  # returns False
print(find(a, 12))  # returns False
print(find(a, 2))  # returns False
print(find(a, 1))  # returns True