Im making an address book and i got a problem. It's telling me that I have a problem with str and lists. I'm new to python so i dont really know what to do.
Here is the full program:
print("\n This is your address book.")
import os
import time
saveFile = open("ContactList.txt", "r")
ContactsString = saveFile.read()
saveFile.close()
Contacts = ContactsString.split('\n')
while '' in Contacts:
Contacts.remove('')
def printContacts():
index = 1
for contact in Contacts:
print("{}: {}".format(index, contact))
index = index + 1
def addContact():
print("You have chosen to add a new contact into your address book.")
name = input("What's the contacts name? ")
counter = Contacts.count(name)
if counter == 0:
address = input("Enter the address of " + name + ': ')
email = input("Enter the email of " + name + ': ')
Contacts.append([ name, address, email ])
else:
print("The contact already exists in the address book.")
print("If you desire to change the contacts current information, enter nr 3 in the menu.")
def browseContacts():
print("You have chosen to view all your contacts with their name, address and email." "\n")
for i in range(0, len(Contacts), 3):
print(Contacts[i], Contacts[i + 1], Contacts[i + 2], '\n')
def searchContacts():
print("\n You have chosen to search up a specific contact.")
searchName = input("Enter the name of the contact that you want to search up: ")
searchFound = False
for i in range(0, len(Contacts), 3):
if searchName == Contacts[i]:
searchName = True
break
if searchFound == True:
print(Contacts[i], Contacts[i + 1], Contacts[i + 2], "\n")
else:
print("We couldn't find " + searchName + " in the address book.")
print("Check whether your spelling was correct or not.")
time.sleep(4)
def deleteContact():
print('\n You have chosen option 4, to delete a specific contact from your address book')
printContacts()
deletename = input(' Enter the name of the contact you wish to delete from your address book: ').title()
deletefound = False
for i in range(0, len(Contacts), 3): #finds the contact the user wants to delete. It only iterates through each third post so that it only goes through the names of the contact.
if deletename == Contacts[i]:
deletefound = True
break
if deletefound == True:
Contacts.pop(i) # deletes the contact information from the list
Contacts.pop(i)
Contacts.pop(i)
print('\n ' + deletename + 'has been removed from your address book')
else:
print('\n ' + deletename + "doesn't exist in the address book")
def modifyContacts():
print("You have chosen option 5, to update a specific contact in your address book.")
printContacts()
newName = input("Enter the name of the contact you wish to update: ")
modifyFound = False
for i in range(0, len(Contacts), 3):
if newName == Contacts[i]:
modifyFound = True
break
if modifyFound == True:
Contacts[i] = input(' Enter the new name of ' + newName + ': ')
Contacts[i + 1] = input(' Enter the new address of ' + Contacts[i] + ': ')
Contacts[i + 2] = input(' Enter the new e-mail of ' + Contacts[i] + ': ')
print('\n The contact has now been updated')
else:
print("\n " + newName + " doesn't exist in the address book")
print(" You can add " + newName + "to your address book by pressing 1 in the menu below")
time.sleep(4)
running = True
while running == True:
print('''
===================================================================
Menu:
1: Add new contact (press 1)
2: Show all contacts (press 2)
3: Show a specific contact (press 3)
4: Delete contact (press 4)
5: Update contact (press 5)
6: Quit program (press 6)
===================================================================''')
actionchoice = input('\n Please enter your choice of action: ')
if actionchoice == '1':
addContact()
elif actionchoice == '2':
browseContacts()
elif actionchoice == '3':
searchContacts()
elif actionchoice == '4':
deleteContact()
elif actionchoice == '5':
modifyContacts()
elif actionchoice == '6':
print('\n Shutting down. Thank you for using the adressbook! \n')
running = False
else:
print('\n The entered command does not exist within this program')
print(' Please enter one of the actions shown in the menu: \n')
time.sleep(4)
saveFile = open("ContactList.txt", "w")
for x in range(0, len(Contacts), 3):
saveFile.write("\n" + Contacts[x] + "\n")
saveFile.write( Contacts[x + 1] + "\n")
saveFile.write( Contacts[x + 2] + "\n")
saveFile.close()
The problem is in the end and the problem says:
Exception has occurred: TypeError
**can only concatenate str (not "list") to str**
File "C:\Users\Anvandare\Documents\Programmering1\Arbetet\addressbook.py", line 162, in <module>
saveFile.write("\n" + Contacts[x] + "\n")
In other words, this is the problem:
saveFile.write("\n" + Contacts[x] + "\n")
What am i supposed to do for it to work?
This line is wrong:
Contacts.append([ name, address, email ])
This is not appending each variable as a separate element of the list, it's creating a nested list in Contacts. It should be:
Contacts.append(name)
Contacts.append(address)
Contacts.append(email)
or:
Concacts += [ name, address, email ]
You also need to fix printContacts() to process Contacts in groups of 3, like the rest of the code.
def printContacts():
index = 1
for i in range(0, len(Contacts), 3):
print("{}: {} {} {}".format(index, *Contacts[i:i+3]))
index = index + 1
IMHO it would be better for Contacts to be a list of dictionaries, but doing this would require a rewrite to 90% of your code and I'm not going to do that here.
Related
I am trying to improve this program. I am working with Linux. I want to add a menu function where the user can pick an option and based on the option call the respective function, but the program is not working, when I run it in the Terminal it doesn't do anything and doesn't show any errors. Please, I need help to solve the problem and make the program works. Thanks!
Here's what I have so far, still there are some functions that need to develop:
#! /usr/bin/python3
import sys
def menu(self):
print ("""
1. Add an Expense
2. Remove an Expense
3. Add revenue
4. Remove Revenue
5. Exit
""")
option = input ("What would you like to do [Number Only]?")
if option == "1":
self.add_expense()
elif option == "2":
self.remove_expense()
elif option == "3":
self.add_revenue()
elif option == "4":
self.remove_revenue()
else:
self.reset_program()
self.close_program()
return option
def add_expense(self):
def __init__(self):
self.month_balance = 0
self.expenses = 0
self.expense_list = []
self.expense_name = []
self.month_balance_name = []
self.month_balance_list = []
self.prompt_income()
def month_balance_ask(self):
add_month_balance = input('Add monthly balance? [y/n]: ')
return add_month_balance
def month_balance_sum(self):
self.month_balance = sum(self.month_balance_list)
def expense_ask(self):
add_expense = input('Add expense? [y/n]: ')
return add_expense
def expense_sum(self):
self.expenses = sum(self.expense_list)
def month_balance_check(self):
if not self.month_balance_list:
print('Please enter at least one monthly balance. ')
self.prompt_month_balance()
def expense_check(self):
if not self.expense_list:
print('Please enter at least one expense. ')
self.prompt_expense()
def prompt_month_balance(self):
x = False
while not x:
result = self.month_balance_ask()
if result == 'y':
month_balance_input = int(input('Enter monthly balance. [Numbers Only]: '))
self.month_balance_list.append(month_balance_input)
month_balance_name = input('Enter monthly balance name. [Name Only]: ')
self.month_balance_name.append(month_balance_name)
else:
self.month_balance_check()
x = True
self.month_balance_sum()
name = [name for name in self.month_balance_name]
month_balance = [month_balance for month_balance in self.month_balance_list]
month_balancedict = dict(zip(name, month_balance))
for k in incomedict:
print(k + ': ', '$' + str(month_balancedict[k]))
print('Total user monthly balance: ', '$' + str(self.month_balance))
self.prompt_expense()
def prompt_expense(self):
x = False
while not x:
result = self.expense_ask()
if result == 'y':
expense_input = int(input('Enter expense amount. [Numbers Only]: '))
self.expense_list.append(expense_input)
expense_name = input('Enter expense name. [Name Only]: ')
self.expense_name.append(expense_name)
else:
self.expense_check()
x = True
self.expense_sum()
name = [name for name in self.expense_name]
expense = [income for income in self.expense_list]
expensedict = dict(zip(name, expense))
for k in expensedict:
print(k + ': ', '$' + str(expensedict[k]))
print('Total user expenses: ', '$' + str(self.expenses))
self.added_expense()
def added_expense(self):
expenseadded = self.month_balance - self.expenses
if expenseadded < 0:
print('You are in the negative, you have a deficit of ' + '$' + str(expenseadded))
if expenseadded == 0:
print('You have broken even, you are spending exactly as much as you make.')
if expenseadded > 0:
print('You are in the positive, you have a surplus of ' + '$' + str(expenseadded))
another = input('Would you like to run another analysis? [y/n]: ')
if another == 'y':
self.menu()
else:
self.reset_program()
self.close_program()
def remove_expense(self):
print("code goes here")
def add_revenue(self):
print("code goes here")
def remove_revenue(self):
print("code goes here")
def reset_program(self):
self.month_balance = 0
self.expenses = 0
del self.expense_list[0:]
del self.expense_name[0:]
del self.month_balance_name[0:]
del self.month_balance_list[0:]
self.prompt_month_balance()
def close_program(self):
print('Exiting Program.')
sys.exit(0)
First time posting so apologizes if formatting is incorrect. My program has 2 lists for now. I I will be adding 4 more after solving this initial issue. One for and item the user selects, and a second with prices for the each item. I have written a code for user selection which runs.
My issue comes with the code for the program associating the item selected with the price list. My first if statement registers item_price as a float, which I gives me what I need. Item_price in the following elif statements are being seen as a list. How can I change them to a float so that the price prints instead of the list?
food=["burger", "pizza", "hotdogs", "tacos"]
food_price=[8.99, 22.50, 3.50, 6.00]
def get_menu_item(item,item_list,):
phrase = "Would you like" + item + "? [y/n] "
response = input(phrase)
if response == "y":
print("Here are your menu options:", item_list)
idx = input("please enter the item you would like from our menu[1,2,3,4]: ")
idx = int(idx) -1
return item_list[idx]
#if user selects [n]
else:
return (None)
#item price function
def get_item_price(item_price,item,item_list):
if item == item_list[0]:
item_price = item_price[0]
elif item == item_list[1]:
item_price == item_price[1]
elif item == item_list[2]:
item_price == item_price[2]
elif item == item_list[3]:
item_price == item_price[3]
return item_price
entree_choice = get_menu_item(" dinner",food)
print('You have selected: ' + entree_choice + ".")
entree_price = get_item_price(food_price,entree_choice,food)
print(entree_price)
I answered this for myself shortly after. I was using == instead of = for all of my elif statements. I feel dumb but writing this out helped me solve it.
You could simplify things further by using a dictionary to store your data :
food_price = {"burger":8.99, "pizza":22.50, "hotdogs":3.50, "tacos":6.00}
def get_menu_item(item,item_list,):
phrase = "Would you like" + item + "? [y/n] "
response = input(phrase)
if response == "y":
print("Here are your menu options:", item_list)
idx = input("please enter the item you would like from our menu[1,2,3,4]: ")
idx = int(idx) -1
return item_list[idx]
else:
return (None)
entree_choice = get_menu_item(" dinner",food)
print('You have selected: ' + entree_choice + ".")
# if entree_choice is not found in the food_price dictionary, the output becomes the string "Entree choice not available."
entree_price = food_price.get(entree_choice, "Entree choice not available.")
print(entree_price)
I'm trying to create a program that will assign a hotel room number to the guests. After it is assigned, inside the dictionary, the value for the key should be updated into either 'Unavailable' or 'Reserved'. But, the thing is, after I close the program. Those room numbers that were updated becomes back into 'Available'.
Is there a way to change the value, so that it stays updated even after I close the program?
import math
f = open('room.txt', 'r+')
#Hotel Rooms
for i in range(1, 7):
locals()['Level{}'.format(i)] = {} # Creating Dictionary
start = 101
for x in range(1, 7):
for i in range(start, start + 10):
eval("Level" + str(x))[i] = 'Available' # Adding Values into Dictionary
start += 100
# heading function
def heading(number, header):
for i in range(60):
print('-', end = '')
print()
for i in range(number):
print('*', end = ' ')
print('\n' + header)
for i in range(number):
print('*', end = ' ')
print()
# customer details function
def customer():
print('''Room Types:
1 --> Single (Level 1)
2 --> Double (Level 2)
3 --> Triple (Level 3)
4 --> Quad (Level 4)
5 --> Queen (Level 5)
6 --> King (Level 6)
''')
room = input('Room Type: ')
name = input('Name: ')
hp = input('Mobile Number: ')
guests = input('Number of Guests: ')
arrival = input('Arrival Date(dd/mm/yyyy): ')
departure = input('Departure Date(dd/mm/yyyy): ')
confirm = input('Confirmation(Y/N): ')
conf(confirm, room, option)
# confirmation function + room for guest
def conf(con, num, opt):
if con == 'Y':
try:
for level, availability in eval('Level' + str(num)).items():
if availability == 'Available':
print('Guest Room Number:', level)
if option == '1':
eval('Level' + str(num))[level] = 'Reserved'
elif option == '2':
eval('Level' + str(num))[level] = 'Unavailable'
print('\nRoom Recorded!')
break
for i in range(1, 7):
f.write('Level ' + str(i) + '\n')
for key in eval('Level' + str(i)):
f.write(str(key) + ": " + eval('Level' + str(i))[key] + '\n')
f.write('\n')
f.close()
except NameError:
print('\nInvalid Room Type.')
elif con == 'N':
print('\nRecord Not Recorded.')
else:
print('\nInvalid Confirmation.')
# sub-headings (options)
def options():
print('''
Choose one of the following options (0-3):
0 <-- Back to Main Menu.
1 --> Booking Engine
2 --> Check In
3 --> Check Out''')
while True:
cus = '* Customer Registration *'
num = math.ceil(len(cus)/2)
heading(num, cus)
options()
option = input('Enter your option: ')
# Booking Engine
if option == '1':
boo = '* Booking Engine *'
num = math.ceil(len(boo)/2)
heading(num, boo)
customer()
#Check In
elif option == '2':
chein = '* Check In *'
num = math.ceil(len(chein)/2)
heading(num, chein)
customer()
else:
print("\nInvalid Option.")
continue
I haven't executed your code, but from what I see :
These are the first lines of your code :
f = open('room.txt', 'r+')
#Hotel Rooms
for i in range(1, 7):
locals()['Level{}'.format(i)] = {} # Creating Dictionary
start = 101
for x in range(1, 7):
for i in range(start, start + 10):
eval("Level" + str(x))[i] = 'Available' # Adding Values into Dictionary
start += 100
Here, you create a dictionary of all rooms, marking ALL of them as Available. It means that whatever the current value of a room, it will be marked as Available again.
Then in the conf function, you re-write the whole file, so the previously Reserved or Unavailable rooms are tagged as Available again.
Here is an example to be clear:
you start the program
You book a room, (e.g room 101), it is marked as Reserved
The rooms.txt file is saved with the correct value.
You stop your program
Then, when starting the program again:
A dict with ALL values set to Available is created. It means that even the room 101 is back to Available
You book any other room (e.g 201)
The dict is saved in your file. BUT remember that as you initialised your dict, all rooms where marked as Available again.
The problem, in fine is that you don't read your rooms.txt file when starting your program to actually load the state of each room in your dictionary.
I also recommend you to drop the custom data structure with which you save the file and go for a JSON file. It will simplify your code.
Code:
import os, csv
def menu():
print("Welcome to da sporty ting" + "\n" + "Menu options: " + "\n")
print("1 - Run the code")
print("2 - Exit")
menu_choice = int(input("Enter choice: "))
while menu_choice not in (1, 2):
menu_choice = int(input("Error, try again: "))
if menu_choice == 1:
finding_file()
elif menu_choice == 2:
exit()
def finding_file():
print("\n")
print("Text file options" + "\n")
print("1 - testfile 1" + "\n" + "2 - testfile 2" + "\n" + "3 - Other")
txt_menu_option = int(input("Enter choice: "))
print("\n")
while txt_menu_option not in (1, 2, 3):
txt_menu_option = input(input("Error, try again: "))
if txt_menu_option == 1:
filename = "Test1_Votes.txt"
pass
elif txt_menu_option == 2:
filename = "Test2_Votes.txt"
pass
elif txt_menu_option == 3:
filename = str(input("Enter name of txt file (don't include .txt at he end) : "))
filename = filename + ".txt"
file_exists = os.path.exists(filename)
if file_exists == False:
print("File does not exist, returning to menu")
menu()
pass
file_validity(filename)
def file_validity(filename):
f = open(filename, 'r+') # opening file in read/write mode
inv_lines_cnt = 0
valid_list = [0, 0, 1, 2, 3] # sorted list of valid values
lines = f.read().splitlines()
f.seek(0)
f.truncate(0) # truncating the initial file
for l in lines:
if sorted(map(int, l.split(','))) == valid_list:
f.write(l + '\n')
else:
print(l + " is a invalid line")
inv_lines_cnt += 1
print("There were {} invalid line/lines.".format(inv_lines_cnt))
calculate_quota(filename)
def calculate_quota(filename):
f = open(filename, 'r+')
lines = f.readlines()
print("Calculate quota " + str(lines))
seats = 2
line_count = 0
for line in lines:
line_count += 1
quota = 0
quota == int((line_count / (seats + 1)) + 1)
print(quota)
quota_required(quota, lines)
def quota_required(quota, lines):
for line in lines:
lines.rstrip(',')
lines.rstrip('\n')
print(lines)
candidate_fp_votes = [0,0,0,0,0]
for line in lines:
for i in range(5):
if line[i] == 1:
print ("if working")
candidate_fp_votes[i] += 1
print (candidate_fp_votes)
print (candidate_fp_votes)
Text file sample:
1,2,3,0,0
0,0,3,2,1
1,0,0,3,2
1,0,0,2,3
0,1,2,3,0
Currently I have a problem where after file_validity(), the text file just appears to have loaded up as empty as when I re-open the file in the next function, it prints lines as empty. file_validity() just deletes the file, and rewrites the valid votes. As you can see I have tried to find out where the trouble lies. I believe the truncate and seek functions seem to be causing some trouble but I am not sure if this is true. And if it were to be the case, how to fix it.
Any help?
Thanks in advance.
So, my code is based of a module in a learning site which I use called Grok, these are the requirements which I had to create in order to pass to the next stage
The Requirements (left-hand-side of photo) and Auto Marker Error (bottom right handside)
The error in question is;
Testing a longer case with many letters. Your submission raised an exception of type ValueError. This occurred on line 4 of program.py.
I can't seem to figure out how to fix this to get it marked correctly, yet my code works pretty much perfectly.
My code
import collections
collect = collections.defaultdict(list)
order = input('Name: ')
name, surname = order.split(' ', 1)
collect[surname].append(name)
mail = open('mail.txt', encoding="UTF-8").read()
mail = mail.split('\n')
letter = 0
package = 0
count = mail.count(' '.join(collect[surname]) + ' ' + surname + ',Letter')
count2 = mail.count(' '.join(collect[surname]) + ' ' + surname + ',Package')
for i in collect:
if (' '.join(collect[surname]) + ' ' + surname + ',Letter') in mail:
letter += 1 * count
if (' '.join(collect[surname]) + ' ' + surname + ',Package') in mail:
package += 1 * count2
if package == 0 and letter == 0:
print("No mail")
if letter > 1:
print(count, "Letters")
if letter == 1:
print(count, "Letter")
if letter == 0 and package != 0:
print("No Letters")
if package > 1:
print(count2, "Packages")
if package == 1:
print(package, "Package")
if package == 0 and letter != 0:
print("No Packages")
There is only one space in the second line of the example for the third input in the screenshot.
For line 4 (name, surname = order.split(' ', 1)), the tuple unpacking will raise a ValueError if there's no space in order.
EDIT:
order = input('Name: ')
# was name, surname = order.split(' ', 1)
try:
name, surname = order.split(' ', 1)
except ValueError:
name, surname = order, ""
collect[surname].append(name)