Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
goto
#1
Smile if (str(k)) in numer:
print ('JÁ EXISTE')
goto to de line that begininig de boddy of program.
the number existe so, dont wrirte and must go to random
i am 72 years old i beginning in basic business in Nixdorf computer.
i am portuguese and dont write correctly de english
thanks
Reply
#2
Esteja ciente de que você pode usar o Google Translate para traduzir do português para o inglês. Isso faz um bom trabalho e ajudará com suas postagens.
Quanto a esta postagem, qual é o valor de k?
a instrução que você mostra, está testando para ver se k, convertido em string está contido em numer, então imprima 'JÁ EXISTE'

Google Translate: https://translate.google.com/
Reply
#3
(Feb-01-2021, 09:49 PM)jmabrito Wrote: Smile if (str(k)) in numer:
print ('JÁ EXISTE')
goto to de line that begininig de boddy of program.
the number existe so, dont wrirte and must go to random
i am 72 years old i beginning in basic business in Nixdorf computer.
i am portuguese and dont write correctly de english
thanks

the variable k comes from a random compare with a txt that has inserted 2 4 9 12 8 3 and if ok is different from any of these numbers it writes, if it should not go to random.The error is that it is always recording whether it already exists or not.
Reply
#4
import random
for r in range(15):
    for i in range(1):    # se estivesse 5 mostrava 5 numeros      
      #print(random.randint(1 ,25))
      k=random.randint(1, 16)
      print (k)
    arq = "bjjj.txt"
    with open(arq ,'r+') as arquivo:
     numer = arquivo.readline()
    if not(str(k)) in numer:
                print ('não EXISTE')
    file1 = open('bjjj.txt', 'a+')
    file1.seek(0)
    file1.write(' ' + str(k))
    file1.close()
   
    if (str(k)) in numer:
                print (' EXISTE')
    print(k)
    print(numer, end='')
the variable k comes from a random compare with a txt that has inserted 2 4 9 12 8 3 and if is different from any of these numbers it writes, if it should not, go to random.The error is that it is always recording whether it already exists or not.
buran write Feb-02-2021, 01:12 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#5
The problem why it always "thinks" number is not in the file, because when you read from file line has new-line char at the end '\n'. so it is never equal.
That said,there is a lot of redundant code in your snippet

the file
Output:
2 4 9 12 8 3
import random
fname = "bjjj.txt"
with open(fname) as f:
    numbers = [int(line) for line in f]
# print the numbers in the file:
print(numbers)

with open(fname, 'a') as f: # open file in append mode
    for r in range(15): # loop 15 times
        k=random.randint(1, 16)
        if k not in numbers:
            print (f'{k} não EXISTE')
            numbers.append(k)
            f.write(f'{k}\n')
        else:
            print (f'{k} EXISTE')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Reading your question a second time, I see that most likely I was wrong as to how the file looks like
I see that in your file numbers are on the same line.
In this case the problem is if not(str(k)) in numer:`

here not(str(k)) is False always. And you condition is actually if False in numer: - which is always evaluated False

Note that you approach is prone to errors e.g. you have 12 in your file. the way you check, even if you fix the condition, will eleminate also 1 and 2 (although 2 is present also separately).

>>> '1' in '2 4 9 12 8 3\n'
True
file:
Output:
2 4 9 12 8 3
import random
fname = "bjjj.txt"
with open(fname) as f:
    numbers = f.readline().strip().split()
# print the numbers in the file:
print(numbers)
for _ in range(15): # loop 15 times
    k = str(random.randint(1, 16))
    if k not in numbers:
        print (f'{k} não EXISTE')
        numbers.append(k)
    else:
        print (f'{k} EXISTE')
with open(fname, 'w') as f: # open file in write mode
    f.write(f'{" ".join(numbers)}\n')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Feb-02-2021, 01:11 PM)jmabrito Wrote:
import random
for r in range(15):
    for i in range(1):    # se estivesse 5 mostrava 5 numeros      
      #print(random.randint(1 ,25))
      k=random.randint(1, 16)
      print (k)
    arq = "bjjj.txt"
    with open(arq ,'r+') as arquivo:
     numer = arquivo.readline()
    if not(str(k)) in numer:
                print ('não EXISTE')
    file1 = open('bjjj.txt', 'a+')
    file1.seek(0)
    file1.write(' ' + str(k))
    file1.close()
   
    if (str(k)) in numer:
                print (' EXISTE')
    print(k)
    print(numer, end='')
the variable k comes from a random compare with a txt that has inserted 2 4 9 12 8 3 and if is different from any of these numbers it writes, if it should not, go to random.The error is that it is always recording whether it already exists or not.
i run your program and
Traceback (most recent call last):
File "C:\Users\José Brito\AppData\Local\Programs\Python\Python39\teste3.py", line 4, in <module>
numbers = [int(line) for line in f]
File "C:\Users\José Brito\AppData\Local\Programs\Python\Python39\teste3.py", line 4, in <listcomp>
numbers = [int(line) for line in f]
ValueError: invalid literal for int() with base 10: '2 3 4 5 6 7 8 9 11 6'
>>>
Reply
#8
(Feb-02-2021, 01:43 PM)buran Wrote: Reading your question a second time, I see that most likely I was wrong as to how the file looks like
I see that in your file numbers are on the same line.
In this case the problem is if not(str(k)) in numer:`

here not(str(k)) is False always. And you condition is actually if False in numer: - which is always evaluated False

Note that you approach is prone to errors e.g. you have 12 in your file. the way you check, even if you fix the condition, will eleminate also 1 and 2 (although 2 is present also separately).

>>> '1' in '2 4 9 12 8 3\n'
True
file:
Output:
2 4 9 12 8 3
import random
fname = "bjjj.txt"
with open(fname) as f:
    numbers = f.readline().strip().split()
# print the numbers in the file:
print(numbers)
for _ in range(15): # loop 15 times
    k = str(random.randint(1, 16))
    if k not in numbers:
        print (f'{k} não EXISTE')
        numbers.append(k)
    else:
        print (f'{k} EXISTE')
with open(fname, 'w') as f: # open file in write mode
    f.write(f'{" ".join(numbers)}\n')

Smile Smile Smile Smile Smile thanks work very well
Reply
#9
Note, my first post assume each number is on separate line in the file (it is mentioned in the post)
Then, on second reading I thought you may have only one line, so look at the snippet in that post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
There's a problem. Even if this number does exist, it continues to a voice that announces that number. pyttsx3
Thank you instruct me that, if "exists" you should go to the FOR, looking for another number. Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make input goto a different line mxl671 2 2,422 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  goto problem Skaperen 1 2,704 Jan-27-2018, 01:11 PM
Last Post: stranac
  Go up script/menu(a goto command) foxtreat 7 8,105 Apr-24-2017, 05:58 PM
Last Post: micseydel
  Is there a goto like there is in BASIC? Luke_Drillbrain 24 14,363 Apr-24-2017, 06:00 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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