• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How would i work this loop out?  RSS feed

 
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need a repeat question to add assignment scores to a module
so if i have a Python module
and issue the students assignments the following should happen

the assignment score cant go over 100 - the loop must then end and the module score must be 100
if a 0 is entered the module is 0 and the loop ends
if say
22 is entered
25 is entered
33 is entered (80 total)
then 21 is entered - the loop must end,and the module total should be 80

this is what i have



im actually more interested how this would be worked out on paper than actually coded

im thinking:

ok if the module total is less than 100 keep running
   if the module total goes over 100 stop running, and subtract the last assignment added from that total (as i have)
   if 0 is entered add 0 as a module total and end the while loop (my tutor doesnt like breaks by the way)
 
i think its just the 0 that is presenting me a problem.
 
Marshal
Posts: 6494
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit weird requirement I'd say.

Why the last mark gets truncated completely? That means student hardly can ever expect 100%.

What I understood from the requirements you have provided, that if score goes over 100, it should become simply a 100 and not for example 121.
But that doesn't mean completely discard it, so it goes back to a moment as you were never took an exam.
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liutaurus

what do you make of this?:

The user will then enter the score for each of the module assignments
 The minimum score per assignment is 0
 The maximum mark per module is 100)
 The number of assignments per module varies (the user will not enter the number of assignments and the program will not assume a number)
Once the score for each of the assignments has been entered the total mark for that module is displayed along with the module grade.
 
Liutauras Vilda
Marshal
Posts: 6494
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case I don't understand the requirements then.

wayne brandon wrote:The user will then enter the score for each of the module assignments


OK, get that.

wayne brandon wrote:The minimum score per assignment is 0


OK, get that.

wayne brandon wrote:The maximum mark per module is 100


OK, get that.

wayne brandon wrote:The number of assignments per module varies


Ok, get that.

wayne brandon wrote:(the user will not enter the number of assignments and the program will not assume a number)


? don't understand what is meant here.

wayne brandon wrote:Once the score for each of the assignments has been entered the total mark for that module is displayed along with the module grade.


Ok..

You still didn't answer why you discard the last score of the module's assignment?


 
Liutauras Vilda
Marshal
Posts: 6494
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why the assignments scores adds up to a total sum of module's sum? Shouldn't be that kind of average?

Because the current logic you have works as this:

Scenario 1
Let's assume there are 4 assignments per module.
1 assignment = 100 score
2 assignment = 0 score
3 assignment = 0 score
4 assignment = 0 score

module score = 100 - is it alright?

Scenario 2
Let's assume there are 4 assignments per module.
1 assignment = 100 score
2 assignment = 100 score
3 assignment = 100 score
4 assignment = 100 score

module score = 100 - is it alright?
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so this is all that has to happen: (Thanks for your help)

if a negative number is entered – the user is finished entering assignments for this module

if a number greater than 100 is entered – the user is finished entering assignments for this module

if a number is entered that brings the total for the module over 100 - – the user is finished entering assignments for this module
 
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand lines 6 and 7. I think you should write out a loop on paper before trying to implement it.
 
Liutauras Vilda
Marshal
Posts: 6494
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand requirements at all. Very confusing and differ per post.

Anyway wayne, you'll have to write your code yourself, so please post what have you got so far. Don't forget to use code tags.
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wasnt 100 percent sure of the requirement myself but after checking with my tutor

any of the following mean that the user has finished entering assignment scores for a module

1) if a negative number is entered – the user is finished entering assignments for this module
2) if a number greater than 100 is entered – the user is finished entering assignments for this module
3) if a number is entered that brings the total for the module over 100 - – the user is finished entering assignments for this module

1) 23
   22
  -5   (Total score is 45)

2) 105
   (Total score is 100)

3) 20
   25
   40
   30 (Total is 85)

 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this seems to meet all the criteria

 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to accomplish this in a loop without using breaks?
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you breaking on an assignmentScore of 0? That is a potential valid mark.
In Java®, I would write something likeI would also spell score correctly.
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes thinking now it should be left alone, only negative numbers should kill the loop.

im trying this

assignmentScore = 0
moduleTotal = 0

while assignmentScore != 0 and moduleTotal < 100:
   assignmentScore = float(input("Enter the assignment score:"))
   moduleTotal += assignmentScore
print(moduleTotal)

but the loop isnt running and i dont understand why

assignmentScore is 0 - while assignmentScore isnt equal to 0 run, it isnt
moduleTotal = 0 - while less than 100 run, it is less than 100

both conditions are true, why does it just end?
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry forgot code tags, pity you cant edit
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
duh sorry, just thinking too much,starting to panic

0 is equal to 0

again, duh
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . . 0 is equal to 0 . . .

Everybody else does that sort of thing, so don't worry. Start assignmentScore with a different value because you are sure to reassign it in the loop, unless you suffer an exception, in which case you want to miss out the whole of the loop, in which case you need to revise your recent thread about exceptions
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey campbell

this looks very good!

thanks!

 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . . this looks very good!

thanks! . . .

That's a pleasure

Yes, doesn't it look a lot better; you are doing the same but in so much less code. Are you adding the exception handling back?
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no its not a requirement for this section

yes its so much prettier than all the ifs and breaks

again thanks i dont know what i would do without Coderanch and Campbell!!!
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got me all embarrassed!

We are good here, and there are lots of other people here who know a lot more than I do.

Is there an elegant way to get rid of line 7?
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does anybody good with python know how this would be simplified



i removed the variable names but it throws an error

 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know; I presume the colon at the end is intended.
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it is

after every loop if statement and function you need :
and you know python works with indentation not {} personally i prefer {}

lol teaching campbell python (i know a good course for 500 dollars!!!) how about a reward??? LOL
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheeky whipper‑snapper! But that sort of cheek deserves something; you are right.
 
wayne brandon
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a tight slap?
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More a byte of very very raw beef.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!