Question

Python: def combo(): play = True while play: x = int(input('How many people\'s information would you...

Python:

def combo():
play = True

while play:
x = int(input('How many people\'s information would you like to see: '))
print()
  
for num in range(x):
name()
age()
hobby()
job()
phone()
print()
  
  
answer = input("Would you like to try again?(Enter Yes or No): ").lower()   
while True:
if answer == 'yes':
play = True
break
elif answer == 'no':
play = False
break
else:
answer = input('Incorrect option. Type "YES" to try again or "NO" to leave": ').lower()

I created this function to create random people with random personalities but what I need help on is saving each person's data in a csv file named data.csv, so that I can integrate it in a GUI.

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for posting the question, here is how you can take inputs from user and write the data to a csv file. In order to write it, you need to store the data in a list and once all data are taken in from user, you pass the list to another function, which actually writes the data to the csv file

I made some changes to your code as it was not working correctly and had missing parts to it.

Here is the code and the screenshot of the demonstration. Thank You !!

__________________________________________________________________________________________________

def write_to_csv_file(data):
    with open('personality.csv','w+') as write_file:
        for row in data:
            line=str(row[0])+','+str(row[1])+','+str(row[2])+','+str(row[3])+','+str(row[4]+'\r\n')
            write_file.write(line)
    print("Data written to file successfully!")

def combo():
    data=[]
    play = True
    while play:
        x = int(input('How many people\'s information would you like to see: \n'))
        for num in range(x):
            name=input("name: ")
            age =input("age: ")
            hobby = input("hobby: ")
            job = input("job: ")
            phone = input("phone: ")
            print (name,age,hobby,job,phone)
            T = name,age,hobby,job,phone
            data.append(T)
        answer = input("Would you like to try again?(Enter Yes or No): ").lower()
        while True:
            if answer == 'yes':
                play=True
                break
            elif answer == 'no':
                play = False
                break
            else:
                answer = input('Incorrect option. Type "YES" to try again or "NO" to leave": ').lower()
    write_to_csv_file(data)
combo()

___________________________________________________________________________________________________

image screenshot

thank you so much : )

Add a comment
Know the answer?
Add Answer to:
Python: def combo(): play = True while play: x = int(input('How many people\'s information would you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Using python 3.6 How do I incorporate 0 or negative input to raise an error and...

    Using python 3.6 How do I incorporate 0 or negative input to raise an error and let the user try again? like <=0 #loop to check valid input option. while True: try: choice = int(input('Enter choice: ')) if choice>len(header): print("Invalid choice!! Please try again and select value either:",end=" ") for i in range(1,len(header)+1): print(i,end=",") else: break choice = int(input('\nEnter choice: ')) except ValueError as ve:print('Invalid response, You need to select the number choice from the option menu') # Catch your...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • Can you please Test and debug the program below using the following specifications? Many thanks! Create...

    Can you please Test and debug the program below using the following specifications? Many thanks! Create a list of valid entries and the correct results for each set of entries. Then, make sure that the results are correct when you test with these entries. Create a list of invalid entries. These should include entries that test the limits of the allowable values. Then, handle the invalid integers (such as negative integers and unreasonably large integers). In addition, make sure the...

  • HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUY...

    HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUYS HELPS ME OUT ON HOW TO TEST A SIMPLE CODE SINCE ITS A GUESSING GAME! THANK YOU. PYTHON import random # here it allows us to use the benefits of the functions random #function 1 which is just a screen organized wordings def screen): screeninstructions () name input("Your Name : ") print('Welcome, name, 'This...

  • Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming...

    Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming skills that you learn in Murach’s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. After you read chapter 2, you can develop a simple program that calculates the batting average for a player. Then,...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT