Am a beginner and I need help

hello am beginner in coding and I need to help to improve this piece of code as am trying to figure out why is nothing happening when the variable accdatabase==1 nothing displays i would rly apreciate any help.
the main probleme is that nothing show up when accdatabase==1 after that the user input 1 in accdatabase=(input(“write 1 for yes or 2 for no”)) or accdatabase=(input(“enter a valid option”))
as it would normaly show a whole bunch of output but instead the code stop
this is the code:

accdatabase=1
volenteer=1.9
payfee=1.9
repeaterror=2
area=1.9
fullname=("sui")
print("hello what are you willing to do")
print("1-Register as a new member of Friends of Seaview Pier")
print("2-sponsoiring the wooden plank")
print("3-display the database")
firstaction=int(input("press either 1,2 or 3 to choose what you want to do"))

while firstaction<1 or firstaction>3:
 firstaction=int(input("choose a valid option"))

if firstaction==1:
 fullname=(input("enter your full name"))
 check=0
 check=check+1

 if check>0.5:
  volenteer=int(input("do you want to volunteer write 1 for yes or 2 for no"))

 while volenteer<0.9 or volenteer>2.1:
  yesorno=int(input("enter a valid data"))

 if volenteer==1:
  print("where is your Area of help")
  print("1-the pier entrance gate")
  print("2-the gift shop")
  print("3-painting and decorating")
  area=int(input("choose either 1,2 or 3 to select your Area of help"))

 while area<1 or area>3:
  area=int(input("choose a valid option"))
 else:
  joindata=(input("enter today's date"))
  print("have you paid the 75$ dollar fee?")
  payfee=float(input("press 1 for yes or 2 for no"))

 while payfee<0.9 or payfee>2.1:
  payfee=float(input("enter a valid data"))
 print("do you want to display the database")
 accdatabase=(input("write 1 for yes or 2 for no"))

 while accdatabase==1 or accdatabase==2:
  accdatabase=(input("enter a valid option"))

if firstaction==2:

  while repeaterror==2:
   sponsname=(input("enter your name"))
   sponssm=(input("enter the short message you want to display on the brass planque"))
   print(sponsname)
   print(sponssm)
   repeaterror=(input("to confirme this choice press 1 if you want to re-enter your information press 2"))

  if repeaterror!=2:      
   print("this will cost you 200$")
   creditcard=(input("enter your credit card information ,200$ will be charged from you"))

if accdatabase==1 or firstaction==3:

 if volenteer==1:
  print("Voulunteers are Richard Ford and",fullname)
 else:
  print("Voulunteers are Richard Ford")

 if volenteer==1 and area==1:
  print("Voulunteers who want to work at pier entrance gate are Richard Ford and",fullname)
 else:
  print("Voulunteers who want to work at pier entrance gate are Richard Ford")

 if volenteer==1 and area==2:
  print("Voulunteers who want to work at the gift shop are Mohamed Ali and",fullname)
 else:
  print("Voulunteers who want to work at the gift shop are Mohamed Ali")

 if volenteer==1 and area==3:
  print("Voulunteers who want to help with painting and decorating tasks are Lebron James and",fullname)
 else:
  print("Voulunteers who want to help with painting and decorating tasks are Lebron James")
 print("Members whose membership has expired are Riyadh Mahrez")

 if payfee==1:
  print("Members who have not yet paid their $75 fee are Cristiano Ronaldo")
 else:
  print("Members who have not yet paid their $75 fee are Cristiano Ronaldo",fullname)

Hi - I will make some comments that may seem harsh, but my intent is to help you improve your skills as fast as possible, so please excuse me for being blunt.

  • The code you have is too verbose and repetitive. This makes it unreadable and difficult to debug.
  • Since your menu options are translated into integers (1, 2, 3), it makes no sense to use comparisons to float values like 0.5 or 2.1. It’s better to simply compare to expected integer values.
  • There are several inconsistencies in rendering (capitalization of menu items) and in naming (“volenteer”, “volunteer” → should be “volunteer”). This comes across as sloppy. Readability of variables names can be improved by using underscores (“first_action” instead of “firstaction”, “acc_database” instead of “accdatabase”, etc).
  • The current code is one big chunk; everything is in the global namespace, there is no high-level, explicit structure. This can be improved a lot by rewriting it with functions.
  • There are too many if/then conditional statements. In this kind of menu program, using match statements leads to much clearer and more readable code.

When rewriting this using functions, you could for instance use (and re-use) a function like the following:

def menu(title: str, options: list[str]) -> int:
    print(title)
    print("-" * len(title))
    print()
    for num, option in enumerate(options):
        print(f"{num + 1}. {option}")
    print()
    selected == -1
    while selected == -1:
        try:
            selected = int(input("Please select one option: "))
            if selected < 1 or selected > len(options):
                print("Invalid option.")
                selected = -1
        except:
            print("Please enter a number.")
            selected = -1
    return selected

This would be called in other places as

    selected = menu("Main menu", ["option 1", "option 2", "option 3"])

Followed by something like

    match selected:
       case 1: do_submenu(1)
       case 2: do_submenu(2)
       case _: do_submenu(3)

But a menu is really a mapping of menu options to actions. So as better implementation could be something like this:

from typing import Any, Callable, Tuple

def menu(title: str, options_to_actions: list[Tuple[str, Callable]]) -> Any:
    print(title)
    print("-" * len(title))
    print()
    for num, (option, action) in enumerate(options_to_actions):
        print(f"{num + 1}. {option}")
    selected == -1
    while selected == -1:
        try:
            selected = int(input("Please select one option: "))
            if 1 <= selected <= len(options):
                action = options_to_actions[selected - 1]
                res = action() 
            else:
                print("Invalid option.")
        except:
            print("Please enter a number.")
            selected = -1
    return res  # returns result of the action

This takes as input a title (string) and a list of pairs (string, callable) where the callables are just names of the functions you want to call when the menu item is chosen.

I predict that when you rewrite the code, your original problem will either disappear or you will immediately be able to see what is causing it.

2 Likes

thx you i really appreciate the fact you took the time to help me to improve thx you

The recommended size of indentation is 4 spaces; indenting by only 1 space is just not clear enough.

These lines:

 while area<1 or area>3:
  area=int(input("choose a valid option"))

look like they’e not indented enough.

This line:

accdatabase=(input("write 1 for yes or 2 for no"))

is missing the int, so accdatabase is a currently string.

This omission occurs in some other places too.

And @Hans: don’t use a ‘bare’ except. Specify what you want to catch (except ValueError:) because a bare except will catch any exception, including NameError, which is not helpful if, say, you misspelled a variable name.

1 Like

yes indded it fixed my programe thx so much

@MRAB I wanted to keep things simple and did want to catch any kind of exception (including for instance Ctrl+C hits)… But I agree that it’s better not to use bare excepts, even in this case. (So, is there ever a good case for bare excepts?)

The only time you’d use a bare except is when you want to catch, write to the log, and then re-raise.

Also occasionally with a worker handling arbitrary tasks, which catches
the exception and proceeds to the next task.

But either way, you usually want to preserve the full exception or log
it in detail, not discard it.