2

After running this code , if i put anything either "yes" or "no", its response appears in console, is possible to simply just delete that and just appears "Wrong answer." ?

import random

print("The random number is:")

for x in range(1):
    print(random.randint(1, 6))

while True:

    answer = input("Do you want to roll again? 'yes' or 'no' ")

    print(answer)

    if answer == 'yes':
        for x in range(1):
            print("The new number is:")
            print(random.randint(1, 6))


    elif answer == 'no':
        print("Thanks for playing.")
        break

    else:
        print("Wrong answer.")
        break
2
  • 1
    is it not easier to print the answer only in the wanted cases ? Commented Sep 3, 2017 at 15:22
  • But if the user put anything eihter yes or no , appears in the console Commented Sep 3, 2017 at 16:13

1 Answer 1

1

I suggest to only print answer in the cases Yes or No :

while True:
    answer = input("Do you want to roll again? 'yes' or 'no' : ")

    if answer.lower() in ['y','yes', 'of course']:
        print('Yes')
        for x in range(1):
            print("The new number is:")
            print(random.randint(1, 6))


    elif answer.lower() in ['n', 'no', 'never']:
        print('No')
        print("Thanks for playing.")
        break

    else:
        print("Wrong answer.")

I just changed the possible answers to allow other answers (more flexible case-insensitive, add other words...)

Also, i removed the break in else statement, i think you should let a chance to users who made typos :)

(I'm not sure you need a for loop to get the random number)

Sign up to request clarification or add additional context in comments.

2 Comments

Works perfectly , just like i want to, i just add a "break" after print "("Wrong answer.")". if i not put that break was print again "Do you want to roll again?"
@AndréMartins thank for the feedback, only you can decide what users are allowed to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.