0

So im trying to make a code that takes an number input from the user and with that input it will display a x number of other inputs to fill in data to be writed it down on a .txt file. But i can't really make it stop showing even with a if x > f: break. Im still learning python and got this far with my little project.

f = file.write(input("Probes:"))
if f == 1:
    file.write(" One Single Soil Probe")
else:
    file.write(" Multiple Soil Probes")
file.write(" \n")

# System
x = 1
for a in range(f):
    file.write("SP" + str(x) + ": ")
    file.write(input("SP " + str(x) + ": " + " \n"))
    file.write("m")
    x = x + 1
    if f > x:
        break
    else:
        continue
file.write(" \n")

file.close()
4
  • 1
    You should get rid of x, and use the variable a. a will start at 0 and go up to the value of f I believe Commented Dec 23, 2021 at 14:17
  • 1
    The input function produces a string. So even if the user types in a number you need to convert it to a number. Commented Dec 23, 2021 at 14:17
  • comparison is not good. Also the range. Personally by looking at the code don't understand either how many iterations you need. Please explain Commented Dec 23, 2021 at 14:18
  • So mainly i want the user to input how many iterations he needs to input data Exemple how i want it to run: Probres: 2 SP 1: (put info here) SP 2: (Put info here) Close Commented Dec 23, 2021 at 14:24

2 Answers 2

1

Your problems start with this line:

f = file.write(input("Probes:"))

Firstly, input() always returns a string, so if you want to compare it to integers later on you'd need to cast it using int().

However, the bigger problem is that you're not assigning the return value of the input() statement to f, you're assigning the return value of file.write(). Something like this will serve you better:

f = int(input("Probes:"))
file.write(f)
...

Also, as explained in the comments above, you don't need x, since you're already using a. Keep in mind, however, that range() starts producing values at 0 unless told otherwise.

Sign up to request clarification or add additional context in comments.

Comments

0

There are a number of issues with the code as presented.

First of all, while file.write may return a number, it's not a useful number in this context: it returns the number of bytes written to the file. SO that's the first change you need to make.

f = input("Probes:")

Howewver, input returns a string, so we need to convert that to a number. If we ignore error checking, it's quite simple:

f = int(input("Probes:"))

Now we've lost the file.write call, so we need to add that back in. However, file.write takes a string:

f = int(input("Probes:"))
file.write(str(f))

The next bit is OK now:

f = int(input("Probes:"))
file.write(str(f))

if f == 1:
    file.write(" One Single Soil Probe")
else:
    file.write(" Multiple Soil Probes")
file.write(" \n")

Next we have your processing loop. You really shouldn't be combining reads and writes like that, it makes it difficult to understand what's going on. I'd re-write it something like this:

for a in range(f):
    sp_val = input("SP " + str(a+1) + ": " + " \n")
    file.write("SP" + str(a+1) + ": " + sp_val + "m\n")
file.write(" \n")

file.close()

Then there's a couple of tricks you can use to make it even cleaner. First is the with statement, secondly is fstrings:

with open(filename) as file:
    f = int(input("Probes:"))
    file.write(str(f))

    for a in range(f):
        sp_val = input(f"SP {a+1}:  \n")
        file.write(f"SP{a+1}: {sp_val}m\n")
    file.write(" \n")

The final point I'd make is to rename some of the variables to make it clearer what they do, and to avoid hiding builtin functions:

with open(filename) as ouput_file:
    probe_num = int(input("Probes:"))
    ouput_file.write(str(probe_num))

    for a in range(probe_num):
        sp_val = input(f"SP {a+1}:  \n")
        ouput_file.write(f"SP{a+1}: {sp_val}m\n")
    ouput_file.write(" \n")

3 Comments

Very interesting, but there's a little problem now that im recieving an error: Traceback (most recent call last): File "C:\Users\Eng5\PycharmProjects\pythonProject1\TxtFacil.py", line 39, in <module> file.write(f) TypeError: write() argument must be str, not int
Good catch, I missed that. Fixed.
Ty sir! U just teached me new stuff and made my 2 day struggle with this part to be over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.