Python Forum
read words from the keyboard and store them
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read words from the keyboard and store them
#1
Huh
Write a program that will read words from the keyboard and store them in a list until the word end
He then randomly selects 3 of them and prints them.
It must:
1.If the user types more than one word and then Enter, do not save the phrase but display the message Only one word at a time.
2.If the total number of words is less than 5, do not randomly select 3 of them but display the message, Very few words.
3.When entering the words if the user again gives the same word he will not accept it but will make a message You have retrieved this word
4.If the end word is given with any combination of case-capitals to terminate the data entry.


the deadline expires in 4 hours.
Reply
#2
The assignment is presented to you, expecting that you will do your best to solve:
We will not write it for you, but will be glad to help when you get stuck.
Try your best to write the code, then:
  • Post your code in code tags
  • Include any error tracebacks, complete unaltered in error tags
  • Tell us where you are having difficulty

we will be glad to help
Reply
#3
from random import randrange
lista2=[]
a=True
word=0
i=0
while True:
    i=i+1
     b=input('give a word:')
    if b==('',' ',''):
        print('one word only.')
    lista2.append(b)     
    b.lower()
    if b==lista2[i]:
        print('the word has been given')
    if b=='end':
        break
    word==word+1
if word<5:
    print('very short number of words')
print(b)
print (word)
print (lista2)
This is what i have done ...
Reply
#4
are you getting error message?
what is the specific problem?
Reply
#5
thank you for the information
this is the error message
IndexError: list index out of range
Δωσε μια λεξη:g
Traceback (most recent call last):
File "C:/Users/damianos/Desktop/emmanouilougeorgia1.py", line 13, in <module>
if b==lista2[i]:

also, i cant find the way how to find if there is a gap between the words
Reply
#6
Here are some of the things that were incorrect:
from random import randrange
lista2 = []
a = True
# Set word to empty string
word = ''
i = 0

# made get_word a function as it needs to be called several times
def get_word():
    while word == '':
        # don't need i here
        # i=i+1
        # indentation was off on next line, added space after :,
        # added lower here rather than later, added spaces between operators
        b = input('give a word: ').lower()

        # following can be done better and needs to be redirected to run again:
        # if b == ('',' ',''):
        #     print('one word only.')
        word = b.split()
        if len(word) > 1:
            print('one word only.')
            # Set word back to '' so loop continues
            word = ''
        print('the word has been given')
    return word

# lista2.append(b)     
# b.lower()
# if b==lista2[i]:

# if b=='end':
#     break
# word==word+1
# if word<5:
#     print('very short number of words')
# print(b)
# print (word)
# print (lista2)
This will allow you to call get_word as many times as needed.
I didn't finish, but this will get you started
Reply
#7
That line is always going to fail. The variable i counts the number of times through the loop. You are using it to index the list of words found so far. That list can be no long than the number of times through the loop. Since Python is 0-index (the first item of a list is at index 0, the second at index 1, and so on), the largest index you can use on that list in one less than the number of times through the loop.

If you want the last item in a list, use a negative one for an index (lista2[-1]). However, that's the line where you check if the word has already been entered. So you don't just want to check it against the last word entered, you want to check it against all words entered. That's a task for the 'in' operator (if b in lisa2:).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
thank you very much for help ... i hope the code is working
Reply
#9
Quote:thank you very much for help ... i hope the code is working
It's not, read my post and Ichabod801's post.
I stated:
Quote:This will allow you to call get_word as many times as needed.
I didn't finish, but this will get you started
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,542 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,949 May-15-2020, 04:57 PM
Last Post: snippsat
  Read keyboard until keypress ytav 1 2,227 Jun-09-2018, 01:14 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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