Checking if inputted strings are equal to strings in list? - python

I'm working on a project that involves building a simplified version of a calendar agent that asks the user for when they want to schedule an appointment and does it for them (if that slot is free). This is the code I have so far:
def find_index(val, seq):
for index in range(len(seq)):
place = seq[index]
if place == val:
return index
else:
return int("-1")
def find_val(val, seq):
for ele in seq:
if val == ele:
return True
else:
return False
def init_nested_list(size_outer, size_inner):
cal = []
for outer_index in range(size_outer):
nested_list = []
for inner_index in range(size_inner):
nested_list.append("-")
cal.append(nested_list)
return cal
def get_input(possible_vals, day_or_time_string):
count = 0
if day_or_time_string == "day":
answer = input("What day would you like your appointment? ")
else:
answer = input("What time would you like your appointment? ")
answer = answer.strip()
nested_list = find_val(answer, possible_vals)
while answer in possible_vals:
break
else:
count = count + 1
answer = input("Invalid entry. Please enter a valid day: ")
if count == 3:
print("This is getting silly - still not a valid entry")
answer = input("Please do try to enter a valid day: ")
count = 0
return answer
def book_slot(cal,days_labels, times_labels, day, time): **ignore this function, haven't finished it yet**
find_index(day, days_labels)
def start_scheduler(cal, days_labels, times_labels):
while True:
day = get_input(days_labels, "day")
time = get_input(times_labels, "time")
book_slot( cal, days_labels, times_labels, day, time)
print("--------------------------------- ")
res = input("Did you want to book more appointments (type n for no, any other key for yes)? ")
if res == "n":
break
days_labels= ["Monday","Tuesday","Wednesday","Thursday", "Friday"]
times_labels = ["9","10","11","12","1","2","3","4","5"]
calendar = init_nested_list(len(days_labels), len(times_labels))
print("Welcome to the acupuncture booking system. ")
start_scheduler(calendar, days_labels, times_labels)
This is what the complete output should look like so far:
Welcome to the acupuncture booking system.
What day would you like your appointment? saturday
Invalid entry. Please enter a valid day: Monday
What time would you like your appointment? 24
Invalid entry. Please enter a valid time: 9
---------------------------------
Did you want to book more appointments (type n for no, any other key for yes)?
However, it seems that no matter what I input when the function asks me for the date/time of the appointment, it doesn't check to see if the inputted strings are equivalent to any of the acceptable ones (in the lists days_labels and times labels). Instead it just accepts any second random input to be correct as shown:
Welcome to the acupuncture booking system.
What day would you like your appointment? s
Invalid entry. Please enter a valid day: z
What time would you like your appointment? d
Invalid entry. Please enter a valid day: f
---------------------------------
Did you want to book more appointments (type n for no, any other key for yes)?
What needs to be done in order to have the function check to see if the inputted strings correspond with any of the strings in the days_labels and times_labels lists in order for the user to "book" an appointment?

So you wont to create a function to check if any inputted string have been already used.
There reason your code isnt working correctly is because you tried to check wherethere your counter is up to 3, while it isnt any loop, and thus it only ascends to 1.
To re-arrange that to a correct way for example, you would do this:
while answer not in possible_values:
<your code here>

I didn't test this at all but it should be enough to guide you to fix your incrementing error.
def isValidDayInput(input):
accept = false
# your code here
return accept
def dayInput(count, maxAttempts):
waiting = true
while (waiting && count <= maxAttempts):
answer = promptForInput()
if (isValidDayInput(answer)): # accept returned true during validation
waiting = false # answer is valid so jump out loop
else(): # accept returned false during validation
count += 1
if (!waiting && count == maxAttempts):
print("Too many incorrect attempts. Exit")
else:
print("thank you")

Related

How to limit the time input to only be accpetable within a timeperiod in Python

I have succesfully made a function that asks the user for a valid time by importing the module Datetime. If the user doesn't enter a valid time the question will be asked until he/she does.
But the problem is that im currently running a zoo, who has open between 08:00 - 22:00. How do I extend my code so the user only can enter a valid time between that timeperiod? Help would be appreciated.
Code:
def input_time():
while True:
try:
time = input("What time do you plan to be here? (HH:MM): ")
pattern_time = ('%H:%M')
time = datetime.strptime(time, pattern_time)
except ValueError:
print("Not a valid time,try again")
continue
else:
break
return time
time = input_time()
Sorry no one has answered this. The time_struct provides member fields that you can test:
while True:
try:
t = input("What time do you plan to be here? (HH:MM): ")
pattern_time = ('%H:%M')
t = time.strptime(t, pattern_time)
if not (8 <= t.tm_hour <= 22):
print("The zoo is only open from 8:00 to 22:00")
continue
except ValueError:
print("Not a valid time, try again")
continue
else:
break
return t
Also, be careful with variable names.

why does this python while loop not work in the program?

I'm trying to run the program in the following article:
https://blockgeeks.com/guides/python-blockchain-2/
I've copied all of the code into my Spyder IDE. When i run it there's a while loop which starts up asking the user to choose a number from the list of options it prints.
After selecting a number the program should perform the requested action. When i select it though it just loops back to the start of the while loop.
It appears to be ignoring the rest of the code in the while loop (the if statement part).
Confusingly if i take the parts of the code from the program which are used in the while loop and run them separately they work i.e if i run the below code and select the number 1 for my choice it will run the code in the if statement.
Why would the if statement run here but not in the main program?
#function 1:
def get_user_choice():
user_input = input("enter a number: ")
return user_input
#function 2:
def get_transaction_value():
tx_recipient = input('Enter the recipient of the transaction: ')
tx_amount = float(input('Enter your transaction amount '))
return tx_recipient, tx_amount
while True:
print("Choose an option")
print('Choose 1 for adding a new transaction')
print('Choose 2 for mining a new block')
print('Choose 3 for printing the blockchain')
print('Choose anything else if you want to quit')
user_choice = get_user_choice()
if user_choice == '1':
tx_data = get_transaction_value()
print(tx_data)
Update:
Sorry i realise i may not have been very clear what the problem is.
The above code is part of the code from the entire program and runs as expected in isolation from the main program.
The below code is the entire program from the article in the link. It includes all of the code in the program. If i run this main program the while loop doesn't use the if statement. It appears to just be breaking straight out of the loop after i select 1, 2 or 3 (any other number should break out of the loop anyway).
Here's a link for a screen shot showing what the console looks like after i have selected the number 1 for the option.
https://ibb.co/RNy2r0m
# Section 1
import hashlib
import json
reward = 10.0
genesis_block = {
'previous_hash': '',
'index': 0,
'transaction': [],
'nonce': 23
}
blockchain = [genesis_block]
open_transactions = []
owner = 'Blockgeeks'
def hash_block(block):
return hashlib.sha256(json.dumps(block).encode()).hexdigest()
# Section 2
def valid_proof(transactions, last_hash, nonce):
guess = (str(transactions) + str(last_hash) + str(nonce)).encode()
guess_hash = hashlib.sha256(guess).hexdigest()
print(guess_hash)
return guess_hash[0:2] == '00'
def pow():
last_block = blockchain[-1]
last_hash = hash_block(last_block)
nonce = 0
while not valid_proof(open_transactions, last_hash, nonce):
nonce += 1
return nonce
# Section 3
def get_last_value():
""" extracting the last element of the blockchain list """
return(blockchain[-1])
def add_value(recipient, sender=owner, amount=1.0):
transaction = {'sender': sender,
'recipient': recipient,
'amount': amount}
open_transactions.append(transaction)
# Section 4
def mine_block():
last_block = blockchain[-1]
hashed_block = hash_block(last_block)
nonce = pow()
reward_transaction = {
'sender': 'MINING',
'recipient': owner,
'amount': reward
}
open_transactions.append(reward_transaction)
block = {
'previous_hash': hashed_block,
'index': len(blockchain),
'transaction': open_transactions,
'nonce': nonce
}
blockchain.append(block)
# Section 5
def get_transaction_value():
tx_recipient = input('Enter the recipient of the transaction: ')
tx_amount = float(input('Enter your transaction amount '))
return tx_recipient, tx_amount
def get_user_choice():
user_input = input("Please give your choice here: ")
return user_input
# Section 6
def print_block():
for block in blockchain:
print("Here is your block")
print(block)
# Section 7
while True:
print("Choose an option")
print('Choose 1 for adding a new transaction')
print('Choose 2 for mining a new block')
print('Choose 3 for printing the blockchain')
print('Choose anything else if you want to quit')
user_choice = get_user_choice()
if user_choice == 1:
tx_data = get_transaction_value()
recipient, amount = tx_data
add_value(recipient, amount=amount)
print(open_transactions)
elif user_choice == 2:
mine_block()
elif user_choice == 3:
print_block()
else:
break
[1]: https://i.stack.imgur.com/FIrn7.png
When comparing values, Python takes a stronger route regarding data types than some other languages. That means no string in Python will equal a number.
Or in other terms "1" == 1 will be False.
That means you have to consider that in Python 3 you will receive a string from input() (not necessarily so in Python 2).
You can either compare this directly to another string:
user_choice = input()
if user_choice == "1":
print("You chose item 1")
Or you can convert it into a number first and compare it to a number:
user_choice = int(input())
if user_choice == 1:
print("You chose item 1")
Note that in the former case it might not be robust if the user enters extra spaces and in the latter case it will fail very loudly with an exception if the user doesn't enter an integer (or even nothing at all).
Both ways can be handled with extra code if necessary. In the former case, you can strip whitespace with user_input = input().strip() and in the latter case you can catch the exception with a try ... except ... block.
You have only handled the case for user_choice == '1'. If you enter anything other than 1, the program will return control to the beginning of the while loop.
I'll suggest you use a debugger to see what user_choice is before the if condition. If not, just use prints.
print("user_choice: {}, type: {}".format(user_choice, type(user_choice))

Bank ATM Program login

I want to make this program that acts as a bank, how do I make sure the correct ID number must be entered with the correct pin and have it depending on the id you entered print hello then their name and prompt how much money they have in the bank.
attempts = 0
store_id = [1057, 2736, 4659, 5691, 1234, 4321]
store_name = ["Jeremy Clarkson", "Suzanne Perry", "Vicki Butler-Henderson", "Jason Plato"]
store_balance = [172.16, 15.62, 23.91, 62.17, 131.90, 231.58]
store_pin = [1057, 2736, 4659, 5691]
start = int(input("Are you a member of the Northern Frock Bank?\n1. Yes\n2. No\n"))
if start == 1:
idguess = ""
pinguess = ""
while (idguess not in store_id) or (pinguess not in store_pin):
idguess = int(input("ID Number: "))
pinguess = int(input("PIN Number: "))
if (idguess not in store_id) or (pinguess not in store_pin):
print("Invalid Login")
attempts = attempts + 1
if attempts == 3:
print("This ATM has been blocked for too many failed attempts.")
break
elif start == 2:
name = str(input("What is your full name?: "))
pin = str(input("Please choose a 4 digit pin number for your bank account: "))
digits = len(pin)
balance = 100
while digits != 4:
print("That Pin is Invalid")
pin = str(input("Please choose a 4 digit pin number for your bank account: "))
digits = len(pin)
store_name.append(name)
store_pin.append(pin)
I'm very impressed by how much you've elaborated on your program. Here's how I would view your solution.
So to create a login simulation, I would instead use a dictionary. That way you can assign an ID to a PIN. For example:
credentials = {
"403703": "121",
"3900": "333",
"39022": "900"
}
Where your ID is on the left side of the colon and the PIN is on the right. You would also have to assign the ID to a name that belongs to that ID using, you guessed it, a dictionary!
bankIDs = {
"403703": "Anna",
"3900": "Jacob",
"39022": "Kendrick"
}
Now that you've done that, you can create your virtual login system using if/else control flow. I've made my code like this:
attempts = 0
try:
while attempts < 3:
id_num = raw_input("Enter your ID: ")
PIN = raw_input("Password: ")
if (id_num in credentials) and (PIN == credentials[id_num]):
print "login success."
login(id_num)
else:
print "Login fail. try again."
attempts += 1
if attempts == 3:
print "You have reached the maximum amount of tries."
except KeyboardInterrupt:
print "Now closing. Goodbye!"
Note the try and except block is really optional. You could use the break operator like you did in your code if you wanted to, instead. I just like to put a little customization in there (Remember to break out of your program is CTRL-C).
Finally, Python has a way of making life easier for people by using functions. Notice I used one where I put login(id_num). Above this while loop you'll want to define your login so that you can display a greeting message for that particular person. Here's what I did:
def login(loginid):
print "Hello, %s!" % bankIDs[loginid]
Simple use of string formatting. And there you have it. The same can be done with displaying that person's balance. Just make the dictionary for it, then print the code in your login definition.
The rest of the code is good as it is. Just make sure you've indented properly your while-loop inside the elif on the bottom of your code, and your last 2 lines as well.
Hope I helped. Cheers!

Problems transferring information from one part of a function to another

While working on my program I have run into a problem where the information stored in Menu option 1 is not being transferred to Menu option 2. As you can see it is correctly stored when in menu one. When it returns to go to menu option 2 its like it never went to option 1.
update #1:
some suggestions I've had is to understand scope? from what I can tell the program is not passing the data along to its parent program even though I've typed out return in each of the definitions.
#Must be able to store at least 4 grades
#Each class can have up to 6 tests and 8 hw's
#Weighted 40%*testavg 40% hw average attendance is 20%
#User must be able to input a minimum grade warning
#after each test the your program must calculate the students average and issue warning if necessary
##Define the Modules##
import math
def menu (a): #2nd thing to happen
menuend = 'a'
while menuend not in 'e':
menuend = raw_input("Type anything other then 'e' to continue:\n")
print "What would you like to do ?"
menudo = 0
print "1 - Enter Courses\n2 - Select Course to Edit\n3 - Save File\n4 - Load File\n5 - Exit\n"
menudo = input("Enter Selection:")
if (menudo == 1):
menuchck = 0
menuchck = raw_input("\nYou have entered #1 (y/n)?:\n")
if menuchck in ["Yes","yes","y","Y"]:
x = m1()
else:
print "I'm sorry,",nam,",for the confusion, lets try again\n"
menu()
elif (menudo == 2):
menuchck1 = 0
menuchck1 = raw_input("\nYou have entered #2 (y/n)?:\n")
if menuchck1 in ["Yes","yes","y","Y"]:
x = m2()
else:
print "I'm sorry,",nam,",for the confusion, lets try again\n"
menu()
elif (menudo == 3):
print "Entered 3"
elif (menudo == 4):
print "Entered 4"
else:
print "Anything Else Entered"
def course(): #3rd thing to happen
b = {}
while True:
while True:
print "\n",name,", please enter your courses below ('e' to end):"
coursename = raw_input("Course Name:")
if (coursename == 'e'):
break
will = None
while will not in ('y','n'):
will = raw_input('Ok for this name : %s ? (y/n)' % coursename)
if will=='y':
b[coursename] = {}
print "\n",name,", current course load:\n",b
coursechck = None
while coursechck not in ('y','n'):
coursechck = raw_input("Are your courses correct (y/n)")
if coursechck =='y':
return b
else:
b = {}
print
##Menu Options##
def m1():
a = course()
return a
def m2():
print "Excellent",name,"lets see what courses your enrolled in\n"
print x
return x
###User Input Section###
name = raw_input("Enter Students Name:\n")
a = {}
menu(a)
raw_input("This is the end, my only friend the end")
In your if-elif blocks in the do==1 case, you write m1(), but for the last case, you write x=m1(). You should have the latter everywhere (by typing m1() you only run the function, but do not store the returned x anywhere).
By the way, you can avoid this if-elif confusion using if chck in ["Yes","yes","Y","y"]:

Python function parameter as a global variable

I have written the following function, it takes in a variable input_name. The user then inputs some value which is assigned to input_name. I want to know the best way to make input_name accessible outside of the function. I know that defining a variable as global, inside a function, means that is can be used outside of the function. However, in this case the variable as actually a parameter of the function so I am not sure how to define it as a global variable. I appreciate any help with this, please find the code in question below:
def input(input_name, prompt):
while True:
data = raw_input(prompt)
if data:
try:
input_name = int(data)
except ValueError:
print 'Invalid input...'
else:
if input_name >= 0 and input_name < 100:
print 'Congratulations'
break
input_name = 'Please try again: '
else:
print 'Goodbye!'
break
month = 0
day = 0
year = 0
century = 0
input(month, "Please enter the month (from 1-12, where March is 1 and February is 12): ")
input(day, "Please enter the day (from 1-31): ")
input(year, "Please enter the year (from 0 - 99, eg. 88 in 1988): ")
input(century, "Please enter the century (from 0 - 99, eg. 19 in 1988): ")
A = month
B = day
C = year
D = century
The simplest thing would be to return the value, and assign it outside the function:
def my_input(prompt):
#.. blah blah..
return the_value
month = my_input("Please enter the month")
# etc.
Other people are saying something like this:
def input(prompt):
return value
value = input(param1,param2, ...)
And that's what you really want to be doing, but just so you know, you can use globals() for changing global variables:
def input(input_name, prompt):
globals()[input_name] = value
What you want to do is probably a bad practice. Just return input_name from the input function.
def input(param1,param2):
return value
value = input(param1,param2, ...)

Categories