Word "None" on line below input [duplicate] - python-3.x

This question already has answers here:
Why does my input always display none in python? [closed]
(2 answers)
Closed 23 days ago.
I wrote a simple program to track household items and output to a text file, with a while loop to have it keep running till "exit" is entered. I get the word "None" on the line below the input line when I haven't written it to do so. Here is my code.
HouseHoldItems = open('C:\\_PythonClass\\Assignment03\\HouseHoldItemsAndValue.txt', mode='a')
Items = ''
Value = ''
# while loop to keep program running until user enters "Exit"
while(True):
# Items is the primary variable, determines if user is entering new household item or wants to end the program
Items = input(print("Enter an Item (Or type 'Exit' to quit) : "))
# If statement to determine if user wants to end program
if(Items.lower() == "exit"):
break
# Else statement to write household items to the file, followed by value of items
else:
HouseHoldItems.write(Items + ', ')
Value = input(print("Enter an estimated Value: "))
HouseHoldItems.write(Value + '\n')
HouseHoldItems.close()
Here is a snip of the way the code looks as I input data items. (https://i.stack.imgur.com/CHOWk.png)
Is it part of the while(true) piece that is making it do this? Thanks for the help in advance.

You do not need to use the print function when using the input function.
Use:
Items = input("Enter an Item (Or type 'Exit' to quit) : ")
and:
Value = input("Enter an estimated Value: ")
While we're at it, in test conditions for while and if statements while your code works as is, it is not considered 'pythonic'. The pythonic way is to not use parentheses. ie
While True:
and:
if Items.lower() == "exit":

Related

Nested while loop, why this loop isnn't looping back to top?

I am trying to create a loop, that will rotate among users and get valid inputs. Inputs are dictionary words checked with external two files one containing allowed words and another restricted. It's a game where users have to say a word that starts with ending letter of their opponents' last word. You get up to 3 passes when you are out of words. When all three passes are takes, you lose the game. I have revised the code many times but it is not looping for some reason:
def game_logic(players_data):
"""takes in players data and initiates the game logic.
:param players_data: contains Player objects with name, word_list and pass_taken as attributes.
:type players_data: list containing dictionaries as items.
"""
# Initialise allowed and prohibited words from external file read.
with open("docs/wordDict.txt", "r") as wordDict:
allowed_words = wordDict.read()
with open("docs/excludeWords.txt", "r") as excludeWords:
prohibited_words = excludeWords.read()
game_switch = True
valid_word = False
player_turn = ""
start_letter = ""
while game_switch:
player_turn = players_data[0].name
if not players_data: # iff empty list
print("Something went wrong. I could not find players! Please restart the game.")
break
elif len(players_data) == 1: # when one person is left
print(f"{players_data[0].name} wins.\nCongratulations!\n°°*°°*°°*°°*°°*°°*° ")
print(f"beat all opponents in: {playtime - datetime.datetime.now()}")
break
else:
print(f"\nIt is {player_turn.upper()}'s turn")
# add a copy of first element to the end
players_data.append(players_data[0])
# remove the first element. so that next turn is next ones'.
players_data.remove(players_data[0])
# start the game
while not valid_word:
if not start_letter:
input_word = input(f"please enter a valid word to begin: ")
if input_word.lower() in allowed_words and input_word.lower() not in prohibited_words:
players_data[-1].word_list.append(input_word)
start_letter = input_word[-1].upper()
print(f"\nStarting letter for next player is: {start_letter}")
valid_word = True
return start_letter
else:
players_data[-1].pass_taken += 1
print(f"FOUL!\nThe word was not recognised as a valid word.\nPenalty: 1 pass({3 - players_data[-1].pass_taken})")
print("Turn goes to your opponent.")
valid_word = False
if players_data[-1].pass_taken >= 3:
print(f"LOST!\n{players_data[-1].name} is out of the game")
players_data.pop()
else:
input_word = input(f"please enter a valid word begining with letter {start_letter}: ")
if input_word.lower() in allowed_words and input_word.lower() not in prohibited_words and input_word[0].upper() == start_letter:
players_data[-1].word_list.append(input_word)
start_letter = input_word[-1].upper()
print(f"\nStarting letter for next player is: {start_letter}")
valid_word = True
return start_letter
else:
players_data[-1].pass_taken += 1
print(f"FOUL!\nThe word was not recognised as a valid word.\nPenalty: 1 pass({3 - players_data[-1].pass_taken})")
print("Turn goes to your opponent.")
valid_word = False
if players_data[-1].pass_taken >= 3:
print(f"LOST!\n{players_data[-1].name} is out of the game")
players_data.pop()
continue
´´´´
As Nico238 said in the comments, you break your loop with return statements inside every if/else statement. As this game relies on a logical progression (Whether a player has entered the correct first letter, and if it's in the dictionary) you need to handle loops carefully, preferably making them as flat (not nested) as possible, and of course not breaking out of them if you need to stay in the game loop.
My suggestion would be to remove those return start_letter statements as you already set it to be something other than empty, and this should continue your loop.

loop back to a menu when using a return statement?

I am adding up all the values in a text file and using a return statement to display the value at the end, I am also using that value in another function. So basically I have a function that calls another function.
My problem is that I need to ask the user if they want to go back to the menu again but I can't do that when using a return statement but if I don't use a return statement then I can get the total to go into my other function.
The average_salary function is the one that calls the total_salary function so that it can get the total value.
My code :
with open("Records.txt") as file: # Opens the text file with all the records in it
records = [line.strip() for line in file if not line.startswith("#")] # Creates a list named Records and stores all the records ignoring any lines with a '#'
def main():
print("\nSelect from one of the options below:\n") # Asks the user to select one of the options on the menu
print("Enter '1' to print out the number of records" # Tells the user what will happen if they select 1
"\nEnter '2' to display a list of employees and their respective details" # Tells the user what will happen if they select 2
"\nEnter '3' to display the total salary bill" # Tells the user what will happen if they select 3
"\nEnter '4' to display a report showing the average salary based on the amount of employees" # Tells the user what will happen if they select 4
"\nEnter '5' to add a new employee to the list" # Tells the user what will happen if they select 5
"\nEnter '6' to display a report to show the amount of employees that are in each position type" # Tells the user what will happen if they select 6
"\nEnter '7' to display employees who earn a higher salary than the threshold" # Tells the user what will happen if they select 7
"\nEnter '8' to search for an existing record" # Tells the user what will happen if they select 8
"\nEnter '9' to exit the program\n") # Tells the user what will happen if they select 9
user_choice = input("Please enter the number of the option you would like use: ") # Asks the user to select what they would to do with the program from a selection of options
if user_choice == "1": # Checks to see if the user has entered '1', if the user enters '1' then the code inside this IF Statement will execute
number_of_lines() # Call the number_of_lines function
elif user_choice == "2": # Checks to see if the user has entered '2', if the user enters '2' then the code inside this IF Statement will execute
list_of_employees() # Calls the list_of_employees function
elif user_choice == "3": # Checks to see if the user has entered '3', if the user enters '3' then the code inside this IF Statement will execute
print("\nYour salary total is £", format(total_salary(), ",.2f"), sep='') # calls the total_salary function and separates it with a space
elif user_choice == "4": # Checks to see if the user has entered '4', if the user enters '4' then the code inside this IF Statement will execute
average_salary() # Calls the average_salary function
elif user_choice == "5": # Checks to see if the user has entered '5', if the user enters '5' then the code inside this IF Statement will execute
add_new_employee() # Calls the add_new_employee function
elif user_choice == "6": # Checks to see if the user has entered '6', if the user enters '6' then the code inside this IF Statement will execute
number_of_position_type() # Calls the number_of_position_type function
elif user_choice == "7": # Checks to see if the user has entered '7', if the user enters '7' then the code inside this IF Statement will execute
above_salary_threshold() # Calls the above_user_threshold function
elif user_choice == "8": # Checks to see if the user has entered '8', if the user enters '8' then the code inside this IF Statement will execute
search_for_a_record() # Calls the add_new_employee function
elif user_choice == "9": # Checks to see if the user has entered '9', if the user enters '9' then the code inside this IF Statement will execute
print("\nProgram closed successfully") # Prints a message to the user
exit() # Exits the program
else:
print("\nEnter a number between 1 to 9") # Displays a message to the user if the do not select one of the above options
main()
# End of IF Statement
def number_of_lines(): # Creates a function named 'number_of_lines'
records_count = sum(1 for records_count in open("Records.txt") if not records_count.startswith("#")) # converts the record_count to a sum and calculates the number of lines in the records.txt file, ignoring the commented lines
print("\nThe number of records is:", records_count) # Prints the number of records for the user to see
lines_finished = input("\nAre you ready to return back to the menu?(yes/no): ").lower() # Asks the user for input
if lines_finished == 'yes': # Checks the users input to see if it is 'yes' , if the users input matches the condition then the code inside the IF will run
main() # Returns the user to the main function
else:
number_of_lines() # If the user types anything but yes then the number_of_lines function will run
def list_of_employees(): # Creates a function named 'list_of_employees'
for line in open("Records.txt"): # Opens the records text file
li = line.strip() # Remove any unnecessary spaces
if not li.startswith("#"): # removes any lines that start with a '#'
print(line.rstrip()) # prints the records to the user
employees_finished = input("\nAre you ready to return back to the menu?(yes/no): ").lower()
if employees_finished == 'yes': # Checks if the users input is equal to yes
main() # Returns the user to the main function
else:
list_of_employees() # If the user types anything but yes then the list_of_employees function will run
def total_salary(): # Creates a function named 'total_salary'
with open("Records.txt", "r") as record_file: # Opens the records files as read and creates a variable called record_file
salaries_list = (record_file.read().split()[11::7]) # Creates a variable named salaries_list, the records files is then read in and split. I then tell the program to start on index 11 and to skip the next 7 elements, this happens until the loop is over
salaries = ' '.join(salaries_list) # joins all values into a list
salaries = salaries.replace(',', '') # Replaces all the ',' with spaces
salaries = salaries.split() # Puts all the salaries into a list again
total = 0 # creates a variable called total
for i in salaries: # Creates a for loop to loop the salaries and add them all together
total += int(i) # Increments by one every time adding all the salaries together
return total # returns the total value
def average_salary(): # Creates a function named 'average_salary'
record_count = sum(1 for record in open("Records.txt") if not record.startswith("#")) # converts the record_count to a sum and calculates the number of lines in the records.txt file
print("\nThe amount of employees is:", record_count) # Prints out the number of records and takes away one to take away the heading line inside the text file
salary = total_salary() # Calls the total_salary() function
print("\nThe total salary is: £", format(salary, ",.2f"))
print("\nThe average salary is: £", format(salary / record_count, ".2f")) # Takes the total salary bill and divides it by the amount of lines and formats it to 2 decimal places
average_salary_finished = input("\nAre you ready to return back to the menu?(yes/no): ").lower() # Stores the users input into the average_salary_finished variable and converts it to lowercase
if average_salary_finished == 'yes': # Checks if the users input is equal to yes
main() # Returns the user to the main function
else:
average_salary() # If the user types anything but yes then the average_salary function will run
def add_new_employee(): # Creates a function named 'add_new_employee'
with open("records.txt", "a+") as storing_records: # Opens the record text file for append+ so that anything written to the text file will be written to the end and also I can read the file
last_record = records[-1] # Creates a variable called last_record and stores the values of the records variable inside, then it gets the last value in the list.
print("\nThe last record in the file is:\n" + last_record, "\n" + "\nPlease enter the number that comes after the previous user ID") # Prints the last_record variable
another_record = "y" # Creates a variable called another_Record and sets it to 'y'
while another_record == "y" or another_record == "Y": # Creates a while loop that will keep running as long as the another_Record variable is set to 'y' or 'Y'
employee_number = input("\nEnter your employee number:") # Stores the users input in the employee_number variable
employee_name = input("\nEnter your name:") # Stores the users input in the employee_name variable
employee_age = input("\nEnter your age:") # Stores the users input in the employee_age variable
employee_position = input("\nEnter your position:") # Stores the users input in the employee_position variable
employee_salary = input("\nEnter your salary:") # Stores the users input in the employee_salary variable
employee_years = input("\nEnter the amount of years you have been employed:") # Stores the users input in the employee_years variable
user_input_record = employee_number + ', ' + employee_name + ', ' + employee_age + ', ' + employee_position + ', ' + employee_salary + ', ' + employee_years # Adds all the user inputs together and separates them with comas
storing_records.write(user_input_record + "\n") # Stores the user input in the records text file
another_record = input("\nDo you want to input another record? (yes/no): ").lower() # Asks the user if they want to add another record, if the user types 'y' or 'Y' then the while loops will run again
if another_record == 'yes':
add_new_employee() # If the user types anything but yes then the add_new_employee function will run
else:
main() # Returns the user to the main function
def number_of_position_type(): # Creates a function called number_of_position_type
with open("Records.txt", "r") as record: # Reads in the text file as record
position_type_list = (record.read().split()[10::7]) # Creates a variable named position_type_list, the records files is then read in and split. I then tell the program to start on index 10 and to skip the next 7 elements, this happens until the loop is over
position_type = ' '.join(position_type_list) # joins all values into a list
position_type = (position_type.replace(',', '')) # Replaces all the ',' with spaces
developer = position_type.count("Developer") # Creates a variable called developer and counts the position_type variable to see how many times the word 'Developers' appears
print('%-40s%-0s' % ("The total number of Developers is:", developer)) # Prints out the developer variable
devops = position_type.count("DevOps") # Creates a variable called devops and counts the position_type variable to see how many times the word 'DevOps' appears
print('%-40s%-0s' % ("The total number of DevOps is:", devops)) # Prints out the devops variable
analyst = position_type.count("Analyst") # Creates a variable called analyst and counts the position_type variable to see how many times the word 'Analyst' appears
print('%-40s%-0s' % ("The total number of Analysts is:", analyst)) # Prints out the analyst variable
tester = position_type.count("Tester") # Creates a variable called analyst and counts the position_type variable to see how many times the word 'Tester' appears
print('%-40s%-0s' % ("The total number of Testers is:", tester)) # Prints out the tester variable
designer = position_type.count("Designer") # Creates a variable called designer and counts the position_type variable to see how many times the word 'Designer' appears
print('%-40s%-0s' % ("The total number of Designers is:", designer)) # Prints out the designer variable
position_finished = input("Are you ready to return back to the menu?(yes/no): ").lower() # Stores the user input into the position_finished variable
if position_finished == 'yes': # Checks the users input to see if it equal to 'yes'
main() # Returns the user to the main function
else:
average_salary() # Returns the user to the search_for_a_record function
def above_salary_threshold(): # Creates a function called above_salary_threshold
try: # Tries the code for any errors
user_threshold = int(input("Enter the salary threshold: ")) # Asks the user to input data and stores it as an integer
for line in records: # Creates a for loop
fields = line.split(', ') # Creates a variable called fields and splits the lines that have ','
if user_threshold < int(fields[4]): # Creates an if statement
print('%03i\t\t%-20s£%-10.2f' % (int(fields[0]), fields[1], int(fields[4]))) # Formats the output
above_salary_finished = input("Are you ready to return back to the menu?(yes/no): ").lower()
if above_salary_finished == 'yes': # Checks if the users input is equal to use
main() # Returns the user to the main function
else:
above_salary_threshold() # Returns the user to the above_salary_threshold function
except ValueError: # Catches any value errors
print("You have entered an invalid number") # Prints a message to the user
main()
def search_for_a_record():
try: # Tries the code for any errors
data = open('Records.txt').read().splitlines() # opens the records file and reads and splits the lines and stores it to the data variable
emp_no = input("\nEnter the UserID you would like to search for: ") # Creates a variable called emp_no and asks the user to enter a userID
record = [line for line in data if line.split(',')[0] == emp_no][0] # Creates a for loop to search through the list of records and splits them up and searches each line for the value entered in by the user
print(record) # Prints the records variable
another_record = input("\nDo you want to enter another User_ID?(yes/no): ").lower() # Stores the user input as lowercase
if another_record == 'yes': # Creates an IF statement to see if the user wants to search for another record, if the user selects yes the code in this section will run
search_for_a_record() # Returns to the beginning of the search_for_a_record function
else:
main() # Returns the user to the main function
except IndexError: # Catches any errors
print("\nYou have entered an invalid number") # Prints a message to the user
search_for_a_record() # Returns the user to the search_for_a_record function
users_name = input("What is your name?: ") # Asks the user for their name and stores their input to the users_name variable
print("\nWelcome", users_name) # Prints welcome followed by the name stored in the users_name variable
main() # calls the main function

How to loop through csv and be able to go back a step or proceed line by line?

I'm trying to look up a time for a user. Let's say they input 13(minutes), my code scrolls through the csv and finds each row that has 13 in the time column. It then prints out the row one at a time. I don't know how to allow a user to have the option of revisiting a previous step? My code currently just reverses the order of the csv, starts from the bottom, even if the rows are not the 13 minute- selected rows.
I'm a total newbie so please try to explain as simple as possible.. Thanks
Please see code:
def time():
while True:
find = input("Please enter a time in minutes(rounded)\n"
"> ")
if len(find) < 1:
continue
else:
break
print("Matches will appear below\n"
"If no matches were made\n"
"You will return back to the previous menu.\n"
"")
count = -1
with open("work_log1.csv", 'r') as fp:
reader = csv.DictReader(fp, delimiter=',')
for row in reader:
count+=1
if find == row["time"]:
for key, value in row.items(): # This part iterates through csv dict with no problems
print(key,':',value)
changee = input("make change? ")
if changee == "back": # ISSUE HERE******
for row in reversed(list(reader)): # I'm trying to use this to reverse the order of whats been printed
for key, value in row.items(): # Unfortunately it doesnt start from the last item, it starts from
print(key,':',value) # The bottom of the csv. Once I find out how to iterate through it
# Properly, then I can apply my changes
make_change = input("make change? or go back")
if make_change == 'y':
new_change = input("input new data: ")
fp = pd.read_csv("work_log1.csv")
fp.set_value(count, "time", new_change) # This part makes the changes to the row i'm currently on
fp.to_csv("work_log1.csv", index=False)
print("")
you can always have list that keep last n lines so you can go back using this list, after reading new line just history.pop(0) and 'history.append(last_line)'
or alternatively you can wrap this logic using stream.seek function

Ask for input, stops asking when enter is pressed, then displays an alphabetically sorted list, print each item separately on a diff line

Challenge:
Need to write a program that asks the user to enter some data, for instance the names of their friends. When the user wants to stop providing inputs, he just presses Enter. The program then displays an alphabetically sorted list of the data items entered. Do not just print the list, but print each item separately, on a different line.
y = []
def alfabetisch( element ):
return element
while True:
user_input = input("Prompt: ")
if user_input != "":
y += [user_input]
if user_input == "":
break
for element in y:
y.sort( key= alfabetisch )
print( element )
I've made some changes and it works 'sometimes' now. If I put in the following input at the 'prompt:'
[Great, Amazing, Super, Sweet, Nice] it gives back: [Great, Great, Nice, Super, Sweet] so that is two times 'Great' and leaves out 'Amazing'
But when I give in the following input at the 'prompt:' [Amorf, Bread, Crest, Draft, Edith, Flood] it gives back: [Amorf, Bread, Crest, Draft, Edith, Flood], so with this particular input it does what I wanted it to do.
What am I doing wrong here, can anyone provide some pointers?
The input of a user is a string.
If the user doesn't input anything the output of input() function is '' ( as mentioned in the comments).
If the user inputs more than one item, as you mentioned for instance a list of friend names then iterating over the string will give you all the chars that compose that string.
A more Pythonic way of doing that will be:
user_input = input("Prompt: ")
print('\n'.join(sorted(user_input.split())))

running a while loop in python and functions not defined

Hi I have this code below and I want the while loop to keep on getting an input from the user whenever the user enters an empty value or doesn't input any value, the program should keep on prompting the user to enter at least a single character, but the code seems to run even though I don't enter any value, thus an empty string the code still executes the (function) in the code, and also I get error "Function not defined"
word = ""
while True:
if word != "":
def str_analysis(string):
if string.isdigit():
if int(string) > 99:
print (string,"Is a big number")
else:
print(string,"Small number")
elif string.isalpha():
print(string,"Is all alphabetical characters")
else:
print(string,"is multiple character types")
word = input ("Enter a word or integer:")
break
str_analysis(word)
I don't know what you expect to happen. word is equal to "" so the if block won't run, so the function won't get defined. Next, we ask the user for input and after that break the loop. Then you try and call a function that was never defined.
What you wanna do is put a break at the end of the function and get rid of the existing one.

Resources