Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help
#1
print("would you like a new list")
t = input("yes/y no/n :")
if t == "n":
    print("OK then have a good day")
if t == "y":
    ty = input("ok enter list entities")
    aee2 = input("1. ")
    bee2 = input("2. ")
    cee2 = input("3. ")
    dee2 = input("4. ")
    eee2 = input("5. ")
    fee2 = input("6. ")
    gee2 = input("7. ")
    hee2 = input("8. ")
    iee2 = input("9. ")
    jee2 = input("10. ")
    kee2 = input("11: ")
    lee2 = input("12: ")
    list3 = (aee2, bee2, cee2, dee2, eee2, fee2, gee2, hee2, iee2, jee2, kee2, lee2,)


print("would you like to check your list for any certain entities")
t = input("yes/y no/n :")
if t == "n":
    print("OK then have a good day")
if t == "y":
    ty = input("ok what would you like to check for: ")
    if ty in list3:
        print("yes that is in the list")
        if ty not in list3: # problem with list3
            print("sorry that number is not in the list")
Yoriz write Jul-20-2021, 07:08 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
if you choose n
if t == "n":
    print("OK then have a good day")
then list3 is not defined and you will get the error
Error:
NameError: name 'list3' is not defined

if ty in list3:
    print("yes that is in the list")
    if ty not in list3: # problem with list3
        print("sorry that number is not in the list")
there is a check that ty in list3,
when that is found to be True it prints yes that is in the list
there is now a check to see if ty is not in list3, but its already known that ty is in list3so the following print will never happen.

It is possible to make a lot of improvements in the way you are coding, are you at a level you would be interested in knowing about them?
Reply
#3
Is there a question coming?

There are easier ways to get the input. This uses a list comprehension to input a list of 4 integers.
mylist = [int(input(f'{i:>2}: ')) for i in range(1, 5)]

print(mylist, sum(mylist))
You can also reduce the code a bit if you decide a y/n response is either y or not y. Right now you give the user three choices: entery y, enter n or enter something else.
mylist = [input(f'{i:>2}: ') for i in range(1, 5)]

if input('Would you like to find something (y/n)? ') == 'y':
    if input('What are you looking for? ') in mylist:
        print('Found it!')
    else:
        print('Is it behind the milk?')
Notice that this shorter version also fixes your logic error where you don't print anything if ty is not in list3. Your version will never print "sorry that number is not in the list" because the only way to reach that code is find ty in list3 then not find ty in list3.
    if ty in list3:
        print("yes that is in the list")
        if ty not in list3: # problem with list3  # Only get here if ty is in list3
            print("sorry that number is not in the list")  # Only get here if ty is not in list3
Your program could be written to make searching the list independent of entering the list.
list3 = []

if input("would you like a new list (y/n)? ") == 'y':
    list3 = [input(f'{i+1:>2}: ' for i in range(11))]
if input("would you like to check your list for any certain entities (y/n)? ") == 'y':
    if input("ok what would you like to check for: ") in list3:
        print("yes that is in the list")
    else:
        print("sorry that number is not in the list")
else:
    print("OK then have a good day")
Or dependent on entering a list:
if input("would you like a new list (y/n)? ") == 'y':
    list3 = [input(f'{i+1:>2}: ' for i in range(11))]
    if input("would you like to check your list for any certain entities (y/n)? ") == 'y':
        if input("ok what would you like to check for: ") in list3:
            print("yes that is in the list")
        else:
            print("sorry that number is not in the list")
    else:
        print("OK then have a good day")
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020