Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Is there a command that runs a line of code again?

fh1

Coder
Hello! Im currently making a admin panel. Im having trouble finding a command that runs a line of code again.
Code:
kicklogs = []
banlogs = []
enter_user = input("Enter your username please: ")
enter_pass = input("Enter your password please: ")
print(f"Welcome {enter_user}")
selectChoice = input("Press B to look at bans. Press K to look at kicks. Press C to look at command to execute")
if selectChoice == "b" or selectChoice == "B":
  print(f"{banlogs}")
  addban = input("Type in a user(s) to log a ban: ")
  append.banlogs(addban)
  print(f"You have logged {addban}.")
if you dont know what i mean then i'll explain. So i want the last line of code to tun 6 again. Please tell me if there is a command or something for that
 
Solution
Here is another way.
Using the match case statement and menu options
Python:
# Create a dict for our menu 
menu = {
    1: 'ban',
    2: 'kick',
    3: 'view ban',
    4: 'view kicked',
    5: 'exit'
}

# Create the list for banned and kicked users
banned = []
kicked = []

# Get username and password
user = input('Enter username: ' ).lower()
passwd = input('Enter password: ')
print()

# Start the loop
while True:
    # Print out the menu options
    print('\nMenu:')
    for key, value in menu.items():
        print(f'{key} -> {value}')
    print()

    # Using a try block to throw an error if anything other than numbers are used
    try:
        command = int(input('Enter a command number: '))
        print()
    except...
Maybe try like this.
Python:
kicklogs = []
banlogs = []
enter_user = input("Enter your username please: ")
enter_pass = input("Enter your password please: ")
print(f"Welcome {enter_user}")
selectChoice = input("Press B to look at bans. Press K to look at kicks. Press C to look at command to execute")
while selectChoice == "b" or selectChoice == "B":
  print(f"{banlogs}")
  addban = input("Type in a user(s) to log a ban: ")
  append.banlogs(addban)
  print(f"You have logged {addban}.")
  selectChoice = input("Press B to look at bans. Press K to look at kicks. Press C to look at command to execute")
Or maybe like this.
Python:
kicklogs = []
banlogs = []
enter_user = input("Enter your username please: ")
enter_pass = input("Enter your password please: ")
print(f"Welcome {enter_user}")
selectChoice = input("Press B to look at bans. Press K to look at kicks. Press C to look at command to execute")
if selectChoice == "b" or selectChoice == "B":
  print(f"{banlogs}")
  count = 0
  while count < 4:
    addban = input("Type in a user(s) to log a ban: ")
    append.banlogs(addban)
    print(f"You have logged {addban}.")
    count=count+1
In that second one you may try less than an end value, for example 4 for 4 times. X E.
 
Here is another way.
Using the match case statement and menu options
Python:
# Create a dict for our menu 
menu = {
    1: 'ban',
    2: 'kick',
    3: 'view ban',
    4: 'view kicked',
    5: 'exit'
}

# Create the list for banned and kicked users
banned = []
kicked = []

# Get username and password
user = input('Enter username: ' ).lower()
passwd = input('Enter password: ')
print()

# Start the loop
while True:
    # Print out the menu options
    print('\nMenu:')
    for key, value in menu.items():
        print(f'{key} -> {value}')
    print()

    # Using a try block to throw an error if anything other than numbers are used
    try:
        command = int(input('Enter a command number: '))
        print()
    except:
        print(f'\nThat is not a valid input.\nPlease enter a number from 1 - {len(menu)}')
    else:
        # Using match for commands
        match command:
            case 1:
                name = input('Enter a user to ban: ')
                banned.append(name)
                print(f'{name} has been banned')
            case 2:
                name = input('Enter a user to kick: ')
                kicked.append(name)
                print(f'{name} has been kicked')
            case 3:
                print('\nBanned Users')
                print(', '.join(banned) if banned else 'No users have been banned.')
            case 4:
                print('\nKicked Users')
                print(', '.join(kicked) if kicked else 'No users have been kicked.')
            case 5:
                print('Goodbye!')
                break
            case _:
                print(f'\nThat command does not exist\nPlease enter a number from 1 - {len(menu)}\n')
 
Last edited:
Solution
Here is another way.
Using the match case statement and menu options
Python:
# Create a dict for our menu
menu = {
    1: 'ban',
    2: 'kick',
    3: 'view ban',
    4: 'view kicked',
    5: 'exit'
}

# Create the list for banned and kicked users
banned = []
kicked = []

# Get username and password
user = input('Enter username: ' ).lower()
passwd = input('Enter password: ')
print()

# Start the loop
while True:
    # Print out the menu options
    print('\nMenu:')
    for key, value in menu.items():
        print(f'{key} -> {value}')
    print()

    # Using a try block to throw an error if anything other than numbers are used
    try:
        command = int(input('Enter a command number: '))
        print()
    except:
        print(f'\nThat is not a valid input.\nPlease enter a number from 1 - {len(menu)}')
    else:
        # Using match for commands
        match command:
            case 1:
                name = input('Enter a user to ban: ')
                banned.append(name)
                print(f'{name} has been banned')
            case 2:
                name = input('Enter a user to kick: ')
                kicked.append(name)
                print(f'{name} has been kicked')
            case 3:
                print('\nBanned Users')
                print(', '.join(banned) if banned else 'No users have been banned.')
            case 4:
                print('\nKicked Users')
                print(', '.join(kicked) if kicked else 'No users have been kicked.')
            case 5:
                print('Goodbye!')
                break
            case _:
                print(f'\nThat command does not exist\nPlease enter a number from 1 - {len(menu)}\n')
Thank you! Could you explain the loop? I tried to use it but it seems it doesn’t loop when the user is done with using one of the commands like banning
 
It loops. Having an input statement in the loop pauses it until enter is pressed.

A quick example with comments.
Python:
# Set counter variable
counter = 0

# Infinte loop
while True:

    # Print counter
    print(counter)

    # Input
    n = input('Press enter/return to count - press q enter to exit:\n>> ')
    
    # Increase counter each loop
    counter += 1

    # A ay to exit the loop
    if n == 'q':
        break
 
It loops. Having an input statement in the loop pauses it until enter is pressed.

A quick example with comments.
Python:
# Set counter variable
counter = 0

# Infinte loop
while True:

    # Print counter
    print(counter)

    # Input
    n = input('Press enter/return to count - press q enter to exit:\n>> ')
   
    # Increase counter each loop
    counter += 1

    # A ay to exit the loop
    if n == 'q':
        break
Thanks
 

New Threads

Buy us a coffee!

Back
Top Bottom