Question

1. Write a code snippet Write a code snippet that asks the user to enter a...

1. Write a code snippet Write a code snippet that asks the user to enter a name and a major one at a time and creates a dictionary with keys corresponding to names and values corresponding to majors. Assume that the user enters “alice”, “bio”, “mark”, “engineering”. >>>d = {} >>> your code starts here

2.Based on your code above: >>> print(d)

3.Write a code snippet Write a code snippet that creates a dictionary (records) using keys from a frozenset of numbers (ids) and values from a list of names (students). >>>students = ["alice", "bob", "carol"] >>>ids = frozenset( [123, 124, 125] ) >>>records = { } >>>your code starts here

4. Based on your code above: >>> print(records)

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

CODE:

user = input("Enter name and major one : ").split()#taking user input
d={}#dictionary
#loop to append to dictionary
for i in range(0,len(user),2):
d[user[i]]=user[i+1]
print("The dictionary created is :",d)#printing the dictionary
#students llist
students = ["alice", "bob", "carol"]
#frozen set of ids
ids = frozenset( [123, 124, 125] )
records={}#declaring the dictionary
i=0#index of students
#iterating through the frozen set.
for id in ids:
#appending the records dictionary with id key and student value
records[id] = students[i]
#incrementing the index of students
i+=1
#printing the records.
print("Records are : ",records)

CODE ATTACHMENTS:

.. File Edit Format Run Options Window Help user = input(Enter name and major one ).split() #taking user input d={ } #dicti

OUTPUT:

Enter name and major one : alice bio mark engineering The dictionary created is : {alice: bio, mark: engineering} Rec

Please do comment for any queries.
Please like it.
Thank you.

Add a comment
Know the answer?
Add Answer to:
1. Write a code snippet Write a code snippet that asks the user to enter a...
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
  • /*--------------------------------------------------------------------------- // AUTHOR: // SPECIFICATION: This program is for practicing the use of classes, constructors, //...

    /*--------------------------------------------------------------------------- // AUTHOR: // SPECIFICATION: This program is for practicing the use of classes, constructors, // helper methods, and the this operator. // INSTRUCTIONS: Read the following code skeleton and add your own code // according to the comments //-------------------------------------------------------------------------*/ import java.util.Scanner; public class { public static void main(String[] args) { // Let's make two students using all two constructors // Write code to create a new student alice using constructor #1 //--> Student alice = // Write code to...

  • Your program has a dictionary called classes. The keys in classes are teacher's last names (as...

    Your program has a dictionary called classes. The keys in classes are teacher's last names (as strings), and the corresponding values are the number of students in that teacher's class. For example, if Mr. Brown has 17 students in her class, then 'Brown' might be a key in classes with a corresponding value of 17. Write a few lines in Python that will do the following: Ask the user to enter the last name of a teacher IF that last...

  • Write a program that asks the user for a student name and asks for grades until...

    Write a program that asks the user for a student name and asks for grades until the user enters a non-number input, it should then ask if the user wants to enter another student, and keep doing the process until the user stops adding students. The student’s must be stored in a dictionary with their names as the keys and grades in a list. The program should then iterate over the dictionary elements producing the min, max, and mean of...

  • in python Write a program that keeps a dictionary in which both keys and values are...

    in python Write a program that keeps a dictionary in which both keys and values are strings—names of students and their course grades. Prompt the user of the program to add or remove students, to modify grades, or to print all grades. The printout should be sorted by name and formatted like this: Carl: B- Joe: C Sarah: A Francine: A

  • Python Write a program that asks the user for a zip code and converts it to...

    Python Write a program that asks the user for a zip code and converts it to a postal bar code. A postal bar code uses two types of lines long and short. Since we can't print "long line” we will use: 0 - short line 1 - long line. The digits are represented by bars as explained in the below chart Value Encoding 1 00011 2 00101 00110 01001 01010 01100 10001 8 10010 9 10100 0 11000 The bar...

  • Write a program that asks for 'name from the user and then asks for a number...

    Write a program that asks for 'name from the user and then asks for a number and stores the two in a dictionary [called the_dicr) as key-value pair. The program then asks if the user wants to enter more data [More data ly/n]?) and depending on user choice, either asks for another name-number pair or exits and stores the dictionary key, values in a list of tuples and prints the list Note: Ignore the case where the name is already...

  • a. Write a line of code that asks the user what their favorite number is, that...

    a. Write a line of code that asks the user what their favorite number is, that only accepts numeric input, and saves the answer in variable D1. b. Write a line of code that asks the user what their favorite color is, that only accepts string input, and saves the answer in variable D2. c. Construct a 1x2 string matrix L1 where the string “HYPE” is the first element and the string “EXHAUSTED” is the second element. d. Using the...

  • Create a new program in Mu and save it as ps4.5.1.py and take the code below...

    Create a new program in Mu and save it as ps4.5.1.py and take the code below and fix it as indicated in the comments: # Write a function called phonebook that takes two lists as # input: # # - names, a list of names as strings # - numbers, a list of phone numbers as strings # # phonebook() should take these two lists and create a # dictionary that maps each name to its phone number. For #...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • In a file called First SecondNext.java, write a program that: • Asks the user to enter...

    In a file called First SecondNext.java, write a program that: • Asks the user to enter a string that contains at least five letters. It is OK if your program crashes when the user enters a string with length less than 5. • Stores that string in a variable called s. • Creates a variable called "first", and sets it equal to the first letter of s. • Creates a variable called "second", and sets it equal to the second...

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