Tamsayılarning ro'yxatini qidirish

Python noldan to'qqizgacha besh tasodifiy raqamni yaratadigan kodni yaratishga harakat qilaman va keyin ularni ro'yxatga qo'yaman. Menga dasturning to'liq sonini kiritishiga ruxsat berish va keyin ro'yxatni izlash uchun dastur kerak.

def main():
    choice = displayMenu()
    while choice != '4':
        if choice == '1':
            createList()
        elif choice == '2':
            print(createList)
        elif choice == '3':
            searchList()
        choice = displayMenu()

    print("Thanks for playing!")


def displayMenu():
    myChoice = '0'
    while myChoice != '1' and myChoice != '2' \
                  and myChoice != '3' and myChoice != '4':
         print ("""Please choose
                        1. Create a new list of 5 integers
                        2. Display the list
                        3. Search the list
                        4. Quit
                        """)
         myChoice = input("Enter option-->")

         if myChoice != '1' and myChoice != '2' and \
            myChoice != '3' and myChoice != '4':
             print("Invalid option. Please select again.")

    return myChoice 

import random

def linearSearch(myList):
target = int(input("--->"))
for i in range(len(myList)):
    if myList[i] == target:
        return i
    return -1


#This is where I need it to ask the user to give five numbers 

def createList():
    newList = []
    while True:
        try:
            num = input("Give me five numbers: ")
            num = [int(num) for num in input().split(' ')]
            print(num)
            if any([num < 0 for num in a]):
                Exception

            print("Thank you")
            break
        except:
            print("Invalid. Try again...")

    for i in range(5):
        newList.append(random.randint(0,9))
    return newList


#This is where the user should be able to search the list

def searchList():
    target = int(input("--->"))
    result = linearSearch(myList,target)
    if result == -1:
        print("Not found...")
    else:
        print("Found at", result)

Biroq, foydalanuvchi raqamni kiritganidan so'ng, u ro'yxatni izlamaydi.

1
Python 3.6 dan foydalanayapman!
qo'shib qo'ydi muallif A. Gunter, manba
Python 3.6 dan foydalanayapman!
qo'shib qo'ydi muallif A. Gunter, manba
Men kechirim so'rayman, men uni qo'shishni unutganligimni tushunib etdim, uni ko'rsatish uchun tahrir qildim.
qo'shib qo'ydi muallif A. Gunter, manba
Men kechirim so'rayman, men uni qo'shishni unutganligimni tushunib etdim, uni ko'rsatish uchun tahrir qildim.
qo'shib qo'ydi muallif A. Gunter, manba
Men kechirim so'rayman, men uni qo'shishni unutganligimni tushunib etdim, uni ko'rsatish uchun tahrir qildim.
qo'shib qo'ydi muallif A. Gunter, manba
"Qidirish" va "izlamaydi" degani nimani anglatadi?
qo'shib qo'ydi muallif TigerhawkT3, manba
"Qidirish" va "izlamaydi" degani nimani anglatadi?
qo'shib qo'ydi muallif TigerhawkT3, manba
"Qidirish" va "izlamaydi" degani nimani anglatadi?
qo'shib qo'ydi muallif TigerhawkT3, manba
Python qanday versiyasidan foydalanasiz? ( input 2.6 va 3.3 da har xil ishlaydi).
qo'shib qo'ydi muallif DyZ, manba
Python qanday versiyasidan foydalanasiz? ( input 2.6 va 3.3 da har xil ishlaydi).
qo'shib qo'ydi muallif DyZ, manba
linearSearchni qaerda aniqladingiz?
qo'shib qo'ydi muallif orsonady, manba
linearSearchni qaerda aniqladingiz?
qo'shib qo'ydi muallif orsonady, manba
linearSearchni qaerda aniqladingiz?
qo'shib qo'ydi muallif orsonady, manba

8 javoblar

Birinchidan, linearSearch hech bir joyda aniqlanmagan. Biror joyni belgilagan deb hisoblasangiz, myList ni searchList funksiyasiga o'tishingiz kerak.

1
qo'shib qo'ydi

Birinchidan, linearSearch hech bir joyda aniqlanmagan. Biror joyni belgilagan deb hisoblasangiz, myList ni searchList funksiyasiga o'tishingiz kerak.

1
qo'shib qo'ydi

Birinchidan, linearSearch hech bir joyda aniqlanmagan. Biror joyni belgilagan deb hisoblasangiz, myList ni searchList funksiyasiga o'tishingiz kerak.

1
qo'shib qo'ydi

createlist() is creating a list but searchList() is not having a reference to it. Your searchList() is not taking any argument so linearSearch() does not know which list to search for the number.

linearSearch(),could be defined in a better way:

def linearSearch(myList,target):
    for i,j in enumerate(myList):
        if target == j:
            return i
        else:
            return -1
1
qo'shib qo'ydi

createlist() is creating a list but searchList() is not having a reference to it. Your searchList() is not taking any argument so linearSearch() does not know which list to search for the number.

linearSearch(),could be defined in a better way:

def linearSearch(myList,target):
    for i,j in enumerate(myList):
        if target == j:
            return i
        else:
            return -1
1
qo'shib qo'ydi

Kodingiz bilan bog'liq ayrim muammolar mavjud

  1. In createList function you are asking user to enter five numbers which you are not using anywhere.
  2. In main function you are calling createList() but you are not storing this in any variable. I t should be something like :

    list=createList()

  3. In main function for choice=2 you are printing function itself, instead you should do following:

    print(list)

Asosiy funktsiyaning boshida ro'yxatni e'lon qilishni unutmang. Chunki foydalanuvchi tanlovsiz 2 tanlovni tanlagan bo'lsa, unda xato bo'ladi.

  1. You should pass list in searchList function as following:

    def searchList(list): target = int(input("--->")) try: result=list.index(target) print("Found at", result) except: print("Not found")

1
qo'shib qo'ydi
Bu juda foydali! Rahmat!
qo'shib qo'ydi muallif A. Gunter, manba

Kodingiz bilan bog'liq ayrim muammolar mavjud

  1. In createList function you are asking user to enter five numbers which you are not using anywhere.
  2. In main function you are calling createList() but you are not storing this in any variable. I t should be something like :

    list=createList()

  3. In main function for choice=2 you are printing function itself, instead you should do following:

    print(list)

Asosiy funktsiyaning boshida ro'yxatni e'lon qilishni unutmang. Chunki foydalanuvchi tanlovsiz 2 tanlovni tanlagan bo'lsa, unda xato bo'ladi.

  1. You should pass list in searchList function as following:

    def searchList(list): target = int(input("--->")) try: result=list.index(target) print("Found at", result) except: print("Not found")

1
qo'shib qo'ydi
Bu juda foydali! Rahmat!
qo'shib qo'ydi muallif A. Gunter, manba

Kodingiz bilan bog'liq ayrim muammolar mavjud

  1. In createList function you are asking user to enter five numbers which you are not using anywhere.
  2. In main function you are calling createList() but you are not storing this in any variable. I t should be something like :

    list=createList()

  3. In main function for choice=2 you are printing function itself, instead you should do following:

    print(list)

Asosiy funktsiyaning boshida ro'yxatni e'lon qilishni unutmang. Chunki foydalanuvchi tanlovsiz 2 tanlovni tanlagan bo'lsa, unda xato bo'ladi.

  1. You should pass list in searchList function as following:

    def searchList(list): target = int(input("--->")) try: result=list.index(target) print("Found at", result) except: print("Not found")

1
qo'shib qo'ydi
Bu juda foydali! Rahmat!
qo'shib qo'ydi muallif A. Gunter, manba
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming