Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help
#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


Messages In This Thread
help - by CodingKid - Jul-20-2021, 06:47 PM
RE: help - by Yoriz - Jul-20-2021, 07:15 PM
RE: help - by deanhystad - Jul-20-2021, 08:41 PM

Forum Jump:

User Panel Messages

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