最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Dynamic output based on user input - Stack Overflow

programmeradmin1浏览0评论

I am fresh to coding and python. As a beginner, I've started working on small projects. I've worked on Mosh's dice rolling game with this code:

    import random

    while True:
        choice = input("Roll the dice? (y/n): ").lower()
        if choice == 'y':
            die1 = random.randint(1, 6)
            die2 = random.randint(1, 6)
            print(f'({die1}, {die2})')
        elif choice == 'n':
            print("Thanks for playing!")
            break
        else:
            print("Invalid choice!")`

Short, sweet and simple. He had another version of this project that challenged us to modify the program so the user can specify how many dice they want to roll. In my head, the challenge is looking for a dynamic output based on the number of dice the user specifies they want to roll.

I'm reading that challenge as something like this:

    import random

    while True:
        choice = input("Roll the dice? (y/n): ").lower()
        if choice == 'y':
            userInput = int(input("How many dice would you like to roll? "))
            x >= int(1)
            if userInput == x:
                die1 = random.randint(1, 6)
                print(f'({die1}')
            elif userInput == '2':
                die2 = random.randint(1, 6)
                print (f'({die1}, {die2})')
            elif userInput == '3':
                die3 = random.randint(1, 6)
                print(f'({die1}, {die2}, {die3})')
            elif userInput == '4':
                die4 = random.randint(1, 6)
                print(f'({die1}, {die2}, {die3}, {die4})')
            elif userInput == '5':
                die5 = random.randint(1, 6)
                print(f'({die1}...{die5})')
            elif userInput == '':
        elif choice == 'n':
            print("Thanks for playing!")
            break
        else:
            print("Invalid choice!")`

I'm stuck in a few places. I'm wondering if there is an easier way to produce a dynamic output based on what integer the user enters that doesn't require me to hard-code all of the "die" outputs. Maybe I need a forLoop, a nested loop. I'm not sure.

**P.S. I'm aware that my code seems unfinished because I got stuck and my brain started to glitch.

I am fresh to coding and python. As a beginner, I've started working on small projects. I've worked on Mosh's dice rolling game with this code:

    import random

    while True:
        choice = input("Roll the dice? (y/n): ").lower()
        if choice == 'y':
            die1 = random.randint(1, 6)
            die2 = random.randint(1, 6)
            print(f'({die1}, {die2})')
        elif choice == 'n':
            print("Thanks for playing!")
            break
        else:
            print("Invalid choice!")`

Short, sweet and simple. He had another version of this project that challenged us to modify the program so the user can specify how many dice they want to roll. In my head, the challenge is looking for a dynamic output based on the number of dice the user specifies they want to roll.

I'm reading that challenge as something like this:

    import random

    while True:
        choice = input("Roll the dice? (y/n): ").lower()
        if choice == 'y':
            userInput = int(input("How many dice would you like to roll? "))
            x >= int(1)
            if userInput == x:
                die1 = random.randint(1, 6)
                print(f'({die1}')
            elif userInput == '2':
                die2 = random.randint(1, 6)
                print (f'({die1}, {die2})')
            elif userInput == '3':
                die3 = random.randint(1, 6)
                print(f'({die1}, {die2}, {die3})')
            elif userInput == '4':
                die4 = random.randint(1, 6)
                print(f'({die1}, {die2}, {die3}, {die4})')
            elif userInput == '5':
                die5 = random.randint(1, 6)
                print(f'({die1}...{die5})')
            elif userInput == '':
        elif choice == 'n':
            print("Thanks for playing!")
            break
        else:
            print("Invalid choice!")`

I'm stuck in a few places. I'm wondering if there is an easier way to produce a dynamic output based on what integer the user enters that doesn't require me to hard-code all of the "die" outputs. Maybe I need a forLoop, a nested loop. I'm not sure.

**P.S. I'm aware that my code seems unfinished because I got stuck and my brain started to glitch.

Share Improve this question asked 2 days ago user23661360user23661360 11 New contributor user23661360 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • Note that in each case, you only roll one die. If the user picks 4, die1, die2, and die3 will be undefined. You should store these in a list and use a loop, so you don't need if statements at all. Also, userInput is an integer, so it will never be equal to '2', which is a string. – Tim Roberts Commented 2 days ago
Add a comment  | 

2 Answers 2

Reset to default 0

You need to use a loop.

import random

while True:
    choice = input("Roll the dice? (y/n): ").lower()
    if choice == 'n':
        break
    count = int(input("How many dice would you like to roll? "))
    dice = []
    for i in range(count):
        dice.append( random.randint(1,6) )
    print(dice)

Personally, I think the "do you want to play?" thing is silly. If they didn't want to play, they wouldn't have run the program.

The brief code to create Walrus and utilize it in a one-liner. Utilizing a single line, eliminating the need for append.

Your initial script will look like this:

import random

while (choice := input("Roll the dice? (y/n): ").lower()) != 'n':
    count = int(input("How many dice would you like to roll? "))
    dice = [random.randint(1,6)for i in range(count)]
    print(dice)

Output:

Roll the dice? (y/n): y
How many dice would you like to roll? 3
[6, 3, 2]
Roll the dice? (y/n): y
How many dice would you like to roll? 2
[2, 6]
Roll the dice? (y/n): n
发布评论

评论列表(0)

  1. 暂无评论