0

1st: So i was told to make a code that can store the names of students who have asked to go on a trip up to the maximum number allowed ( 45 students max).

2nd: i must also input and store whether each student has paid.

3rd: enable printouts to be produced to show which students have not paid.

so as of now, im still at the 1st stage. here's my code, i decided to use the dictionary command:

d = {}
d [input("have the student paid?: ")]=input("enter 1st name: ")
d [input("have the student paid?: ")]= input("enter 2nd name: ")
d [input("have the student paid?: ")]= input("enter 3rd name: ")
d [input("have the student paid?: ")]= input("enter 4th name: ")
d [input("have the student paid?: ")]= input("enter 5th name: ")

It worked perfectly fine.

But is there any other way on how to input the names and assign the name correctly to which students have/ have not paid.

Is there any method of doing it instead of writing it one by one, because i need to write 45 of those.

This not my complete answer for the 1st question

I also need extra help for my 2nd and 3rd question please.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41

3 Answers3

1

Have you tried using a for loop?

Also, your dictionary should be the other way around. You have a dictionary of 'Paid?' (which can be either yes or no) to a student's name. However, when you try to print the dictionary, it will have only two members: yes and no, with only two names. That's because you are overwriting the last respective value.

Try something like this:

students = {} # declare a 'students' dictionary

for i in range(45): # this loop will be executed 45 times (from 0 to 44)
  student_name = input('Enter the student\'s name: ') # get the student's name
  input_student_has_paid = input('Has the student paid? Y/N: ') # only accept Y or N as an answer to avoid inconsistencies
  if input_student_has_paid.lower() in ['y', 'yes']: # accept either 'y' or 'yes' (case insensitive because of .lower()
    student_has_paid = True
  else:
    student_has_paid = False
  students[student_name] = student_has_paid # this will be, for example, d['rafael'] = False since I have not paid

print(students)

More about dictionaries.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rafael de Bem
  • 641
  • 7
  • 15
  • @Dharman, I understand that this is not your code but do you know what is the function for writing this down," ['y', 'yes'] ". what effect does it have on the program? I've tried running the program with and without the ," ['y', 'yes'] " and I can't seem to spot the differences. – Jonathan Alie Saputra Oct 09 '20 at 12:58
  • 1
    @JonathanAlieSaputra I do not know Python. Even if I knew Python I have no idea what this function Rafa de boas written does. Please ask the author of this answer for clarifications, not me. I was just editing the answer and I didn't write this answer. I am not the author of the answer as you can see is clearly written under this post. – Dharman Oct 09 '20 at 13:05
  • @JonathanAlieSaputra the `if something in ['y', 'yes']` code is sort of an abbreviation of checking if `something` equals to `'y'` or to `'yes'` (since they are both in that list) – Rafael de Bem Oct 09 '20 at 14:23
0

you can use a for loop for the first part:

d = {}
for number in range(45):
    d[input("have the student paid?: ")] = input(f'enter student {number} name: ')
Rafael de Bem
  • 641
  • 7
  • 15
Arda Kutlu
  • 60
  • 1
  • 7
0

The most pythonic and least readable way to do it is the following:

number_of_students = 45
d = {input("Student name: ") : (True if input("Has paid: ").lower() in "yes" else False) for _ in range(number_of_students)}

Note that the first prompt will be "Has paid" and the second one "Student name", so it's reversed but the same way OP stated it. Also note the prompt "Has paid" will give True for the following answers: "y", "e", "s", "ye", "es", "yes" with all combinations of lowercase and uppercase letters.

Alex Mandelias
  • 436
  • 5
  • 10