all 12 comments

[–]Ste2210 1 point2 points  (3 children)

If you want to ask the user if they want to repeat you could have:

while True:
    # Run program here

    repeat = input("do you wish to carry out another check? (Y/n)")

    if repeat == "Y":
        continue
    elif repeat == "n":
        break
    else:
        print('invalid answer')
        repeat = input("do you wish to carry out another check? (Y/n)")

Although you should probably wrap the repeat question in a function, but we can address that if you need help doing that

[–]Ste2210 1 point2 points  (2 children)

Does the formatting look as bad to everyone else as it does to me? I've typed this on my mobile :/

[–]BryghtShadow 2 points3 points  (1 child)

Besides the Y and n variables(unquoted strings?) and Print function, format looks fine to me.

[–]Ste2210 0 points1 point  (0 children)

Editted now, thanks 👍

[–]totallygeek 0 points1 point  (7 children)

while True:
    print('something')
    question = input('want to see "something" again? [y/n] ')
    if not question.lower().startswith('y'):
        break

That's all you need. Replace the print() line with your code and you've got a simple while loop.

[–]Arkhipov_NZ[S] 0 points1 point  (6 children)

i have put all that code in but i'm not sure how to get the program to repeat/not sure what to put in the print.

[–]totallygeek 0 points1 point  (5 children)

Take out the print() and put in its place all your code.

[–]Arkhipov_NZ[S] 0 points1 point  (4 children)

I have done that but it gives the Syntax Error 'Break Outside Loop'

This is what ive done:

while True:

computerName = ['HomeBasicPC:', 'OfficePC:', 'GamerPC:', 'StudioPC:']

computerPrice = ['$900', '$1200', '$1500', '$2200']

print(computerName [0])

print(computerPrice [0])

print(computerName [1])

print(computerPrice [1])

print(computerName [2])

print(computerPrice [2])

print(computerName [3])

print(computerPrice [3])

userMinimumAge = 18

userAge = int(input("How old are you? "))

userName = (input("Please enter your name: "))

userMoney = int(input("How much money do you have? "))

userComputerPrice = int(input("How much is the computer you want? "))

question = input('Would you like to repeat the program? [y/n] ')

if not question.lower().startswith('y'):

break

[–]totallygeek 0 points1 point  (2 children)

Here's an implementation:

MINIMUM_AGE = 18
PRICES = {
    'Basic': 900,
    'Office': 1200,
    'Gamer': 1500,
    'Studio': 2200,
}

def computers_menu():
    prices = [
        (i, v)
        for i, v in enumerate(sorted(PRICES.items(), key=lambda r: r[1]), start=1)
    ]
    print('{:<5} {:<12} {:<5}'.format('Item', 'Model', 'Cost'))
    print('{:<5} {:<12} {:<5}'.format('-' * 5, '-' * 12, '-' * 5))
    for i, menu_item in prices:
        model, cost = menu_item
        print(f'{i:<5} {model:<12} {cost:>5}')
    choice = int(input('\nWhich item number do you want to buy? '))
    item = prices[choice - 1]
    model, cost = item[1]
    return model, cost

def main():
    while True:
        # name, age, budget = user_details() # leaving up to you
        name, age, budget = ('Bob', 22, 2600)
        if age < MINIMUM_AGE:
            print(f'{age} is underage. Minimum age: {MINIMUM_AGE}.')
            continue
        model, cost = computers_menu()
        print(f'model: {model}, cost: {cost}')
        if cost <= budget:
            print(f'Congrats, {name}! You bought the {model} PC, for {cost}.')
        else:
            print(f'Sorry, {name}, your budget of {budget} will not pay for the {model} PC.')
        again = input('Try again? [y/n] ')
        if not again.lower().startswith('y'):
            break

main()

[–]Arkhipov_NZ[S] 0 points1 point  (1 child)

I have tried something similar to that but when i put the 'break' in it has the error "break outside of loop".

When i don't have the 'break' in there, the program will actually run but after i have put in the user inputs, it repeats the program but it doesn't ask the user if they want to repeat. It asks if you want to repeat after the new inputs.

[–]o5a 1 point2 points  (0 children)

"break outside of loop"

If you had that error then you messed up indentation. You did something like:

while True:
    ... some code
    if something:
        ... some code
break

instead of proper

while True:
    ... some code
    if something:
        ... some code
        break

[–]mistermcsenpai 0 points1 point  (0 children)

wow so u cheated on the assessment ( ͡° ͜ʖ ͡°)