Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Back Again

  • 22-11-2016 1:01pm
    #1
    Banned (with Prison Access) Posts: 613 ✭✭✭


    Hey guys me again
    Feel like I ask you alot, I just find the info you give me super helpful.
    So I have to work out a programme with nested loops. The loops I have to use are For and While and usually I figure out how to get it to work this has me completely bamboozled.
    I need to get enter 5 numbers under 99 and get a average after every number entry I need to valid it is under 99 and greater than 0. Everything Ive tried has failed epically :(


Comments

  • Registered Users, Registered Users 2 Posts: 1,987 ✭✭✭Ziycon


    Can you show us what you have so far and what errors you're getting?


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    Kal El wrote: »
    Hey guys me again
    Feel like I ask you alot, I just find the info you give me super helpful.
    So I have to work out a programme with nested loops. The loops I have to use are For and While and usually I figure out how to get it to work this has me completely bamboozled.
    I need to get enter 5 numbers under 99 and get a average after every number entry I need to valid it is under 99 and greater than 0. Everything Ive tried has failed epically :(

    So you want us to just do your homework for you without you showing us any attempt?


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    So you want us to just do your homework for you without you showing us any attempt?

    What :confused: I have never asked that. I always do my own thing first and see where I stand. What would be the point of getting someone else to do my homework
    The reason I havent posted code is because Im still trying to figure it out by myself, I always post code when I get that far.


  • Moderators, Regional Midwest Moderators Posts: 11,183 Mod ✭✭✭✭MarkR


    OK, break it down to steps, and then figure out the order it needs to be done, and the conditions required to get there.


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    Kal El wrote: »
    What :confused: I have never asked that. I always do my own thing first and see where I stand. What would be the point of getting someone else to do my homework
    The reason I havent posted code is because Im still trying to figure it out by myself, I always post code when I get that far.

    Like has been said, post what you've tried so far. If you can't make sense of it maybe someone else can. From the outside it reads very like you want it done for you.


  • Advertisement
  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Like has been said, post what you've tried so far. If you can't make sense of it maybe someone else can. From the outside it reads very like you want it done for you.

    Nope, sorry if it came across that way. I will post the code when I get it to work :o
    By work I mean work my way. I thought I would have it by now since making the post but other assignments were more pressing :(


  • Closed Accounts Posts: 9,046 ✭✭✭Berserker


    In agreement with the others can you post what you have done so far. It'll be easier for us to give you guidance if you do.


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Berserker wrote: »
    In agreement with the others can you post what you have done so far. It'll be easier for us to give you guidance if you do.
    num1 = int(input("Number 1: "))
    num2 = int(input("Number 2: "))
    num3 = int(input("Number 3: "))
    num4 = int(input("Number 4: "))
    num5 = int(input("Number 5: "))
    while num1 in range(1,100) :
        while num2 in range(1,100):
            while num3 in range(1,100):
                while num4 in range(1,100):
                    while num5 in range(1,100):
                        print(num1+num2+num3+num4+num5)
                        print(num1)
    

    So Ive started this but I know its wrong. I have tried other ways putting the inputs between the loop. Like this is my recent attempt.
    This is obviously just the actual input part. I have been trying to figure that out before going onto the validation.
    I tried it with the for loop aswell.
    Like I know Ive gone massively the wrong way with this, tomorrow I have 5 hours free in the lab so Ill probably figure out where I went wrong and Im probably just complicating everything :o.


  • Registered Users, Registered Users 2 Posts: 6,236 ✭✭✭Idleater


    Kal El wrote: »
    Like I know Ive gone massively the wrong way with this, tomorrow I have 5 hours free in the lab so Ill probably figure out where I went wrong and Im probably just complicating everything :o.

    Out loud speak what you have just typed.

    While something.....

    Ask yourself what is that something, when does that something change and when will it stop.

    As a next step read out loud your assignment. While reading it, make note of each time you are asked to count something.

    That's a start.


  • Closed Accounts Posts: 9,046 ✭✭✭Berserker


    Ok, you seem to be trying to do too many things at once. Take a step back and review the problem. Break it into single steps. Forget about the writing the code.

    Fill out the following. To solve this problem, I need to :

    1) Read in a value a number of times.
    2) Check that it is within a certain range of values.
    3)

    Just one thing, are you required to use a 'while' and 'for' loop or a 'while' or 'for' loop?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    I would break it up even further. You need to think of code like building blocks

    Here are your requirements, try think of code examples that can do each step on their own, then try to combine them.

    Store a number from input

    Maintain a list of previous inputs

    Repeat until I have all the inputs I need

    Check the average of a list of numbers

    Print a number

    These building blocks can be used to create your program.


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Ok so thanks for the replies. It helped me figure out what I was doing wrong :o
    I had misread what he asked for in the assignment, I presumed he wanted Nested but he didnt. I knew what I was doing was wrong :o
    He just wanted code like

    While x <5
    if ----
    --
    --
    else

    Which I know how to do :o


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭Sephiroth_dude


    Is it Java you are writing it in?


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭pillphil


    python, I think


  • Registered Users, Registered Users 2 Posts: 1,465 ✭✭✭Anesthetize


    Kal El wrote: »
    num1 = int(input("Number 1: "))
    num2 = int(input("Number 2: "))
    num3 = int(input("Number 3: "))
    num4 = int(input("Number 4: "))
    num5 = int(input("Number 5: "))
    while num1 in range(1,100) :
        while num2 in range(1,100):
            while num3 in range(1,100):
                while num4 in range(1,100):
                    while num5 in range(1,100):
                        print(num1+num2+num3+num4+num5)
                        print(num1)
    
    Five nested while loops?? If I saw that in a code review I would cry :) Also your use of range() is incorrect.

    Here's a hint: you can achieve what you want to do by using a one while loop nested inside one for loop.


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Five nested while loops?? If I saw that in a code review I would cry :) Also your use of range() is incorrect.

    Here's a hint: you can achieve what you want to do by using a one while loop nested inside one for loop.

    Thanks for the reply buddy, when I re-read the question I figured it out in a few mins. Was doing a assignment for another class and have a teething baby :( so couldnt focus on this one. It was 100% my own fault, felt so stupid when I looked at the question :o


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    Kal El wrote: »
    Thanks for the reply buddy, when I re-read the question I figured it out in a few mins. Was doing a assignment for another class and have a teething baby :( so couldnt focus on this one. It was 100% my own fault, felt so stupid when I looked at the question :o

    Happens to the best of us that does......


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    So Im doing some practice questions for my exams and this one has me thrown.
    january = ("The Next Month Is: February")
    february = ("The Next Month Is: March")
    march = ("The Next Month Is: April")
    april = ("The Next Month Is: May")
    may = ("The Next Month Is: June")
    june = ("The Next Month Is: July")
    july = ("The Next Month Is: August")
    august =("The Next Month Is: September")
    september = ("The Next Month Is: October")
    october = ("The Next Month Is: November")
    november = ("The Next Month Is: December")
    december = ("The Next Month Is: January")
    def calendar():
        month = input("Enter Month: ")
        if month == "january":
            print (january)
        elif month == "february":
            print (february)
        elif month == "march":
            print (march)
        elif month == "april":
            print (april)
        elif month == "may":
            print (may)
        elif month == "june":
            print (june)
        elif month == "july":
            print (july)
        elif month == "august":
            print (august)
        elif month == "september":
            print (september)
        elif month == "october":
            print (october)
        elif month == "november":
            print (november)
        else month == "december":
            print (december)
    calendar()
    

    It all works fine till the else, then I get a invalid syntax :mad:
    Any ideas whats wrong


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    Kal El wrote: »
    So Im doing some practice questions for my exams and this one has me thrown.
    january = ("The Next Month Is: February")
    february = ("The Next Month Is: March")
    march = ("The Next Month Is: April")
    april = ("The Next Month Is: May")
    may = ("The Next Month Is: June")
    june = ("The Next Month Is: July")
    july = ("The Next Month Is: August")
    august =("The Next Month Is: September")
    september = ("The Next Month Is: October")
    october = ("The Next Month Is: November")
    november = ("The Next Month Is: December")
    december = ("The Next Month Is: January")
    def calendar():
        month = input("Enter Month: ")
        if month == "january":
            print (january)
        elif month == "february":
            print (february)
        elif month == "march":
            print (march)
        elif month == "april":
            print (april)
        elif month == "may":
            print (may)
        elif month == "june":
            print (june)
        elif month == "july":
            print (july)
        elif month == "august":
            print (august)
        elif month == "september":
            print (september)
        elif month == "october":
            print (october)
        elif month == "november":
            print (november)
        [B]else month == "december":[/B]
            print (december)
    calendar()
    

    It all works fine till the else, then I get a invalid syntax :mad:
    Any ideas whats wrong

    Why are you running an evaluation on the else statement? Judging by your code, by the time you get to it, it will only be december.

    I'm not a python guru but will the following work
    ..... lots of if statements
    else print (december)
    

    Alternatively, the following will cater for invalid entries...
    january = ("The Next Month Is: February")
    february = ("The Next Month Is: March")
    march = ("The Next Month Is: April")
    april = ("The Next Month Is: May")
    may = ("The Next Month Is: June")
    june = ("The Next Month Is: July")
    july = ("The Next Month Is: August")
    august =("The Next Month Is: September")
    september = ("The Next Month Is: October")
    october = ("The Next Month Is: November")
    november = ("The Next Month Is: December")
    december = ("The Next Month Is: January")
    def calendar():
        month = input("Enter Month: ")
        if month == "january":
            print (january)
        elif month == "february":
            print (february)
        elif month == "march":
            print (march)
        elif month == "april":
            print (april)
        elif month == "may":
            print (may)
        elif month == "june":
            print (june)
        elif month == "july":
            print (july)
        elif month == "august":
            print (august)
        elif month == "september":
            print (september)
        elif month == "october":
            print (october)
        elif month == "november":
            print (november)
        elif month == "december":
            print (december)
        else print ("Invalid input detected")
    calendar()
    


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    Why are you running an evaluation on the else statement? Judging by your code, by the time you get to it, it will only be december.

    I'm not a python guru but will the following work
    ..... lots of if statements
    else print (december)
    

    Thanks buddy, the invalid entry is the perfect way for me :D


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,236 ✭✭✭Idleater


    Kal El wrote: »
    Thanks buddy, the invalid entry is the perfect way for me :D

    Yeah, the way you finished the else actually had an enexpected if like statement (month == December or something).

    That's just python complaining at you, and sometimes it is a bit vague (I program in python for work) so you have to get used to that.


Advertisement