Jump to content

Asking the user for an input until but the answer is not valid.

Hafiz75

So im working on a program and what the program to ask the user to input a file name and if the file name does not exist it should say invalid input please try again. Then it should display the input part again until the file is found. How would i make the program re ask the question. I've put the code below. I would really appreciate if someone could help me figure out how i would go around this problem. 

userinput= input("Whats your File Called? \n" )
            confile=userinput+".txt"
            def main():
                f=open(confile, "r")
                if f.mode == 'r':
                   contents = f.read()
                   print(contents)
            if __name__== "__main__":
              main()

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Put the code requesting and checking the input in a while loop.

 

You'll need to check that the file exists (os.path.exists(FILENAME)) or catch an exception from opening a non-existent file.


# while CONDITION:

    # Input

    # If input == ____

         break

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, 79wjd said:

Put the code requesting and checking the input in a while loop.

I dont understand what you mean?

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, 79wjd said:

Put the code requesting and checking the input in a while loop.

 

 


# while CONDITION:

    # Input

    # If input == ____

         break

 

So you want me to add a while loop to my code?

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Safeer Hafiz said:

I dont understand what you mean?

 

while True:

     user_input = input("Input filename")

     if os.path.exists(user_input):

         break

f = open(user_input)

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Still get an error Message

            while True:
                userinput= input("Whats your File Called? \n" )
                confile=userinput+".txt"
                def main():
                    f=open(confile, "r")
                    if f.mode == 'r':
                       contents = f.read()
                       print(contents)
                if __name__== "__main__":
                  main()
                if os.path.exists(userinput):
                    break
            f = open(userinput)
            

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, 79wjd said:

while True:

     user_input = input("Input filename")

     if os.path.exists(user_input):

         break

f = open(user_input)

Any ideas why i get an error message

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Safeer Hafiz said:

Still get an error Message


            while True:
                userinput= input("Whats your File Called? \n" )
                confile=userinput+".txt"
                def main():
                    f=open(confile, "r")
                    if f.mode == 'r':
                       contents = f.read()
                       print(contents)
                if __name__== "__main__":
                  main()
                if os.path.exists(userinput):
                    break
            f = open(userinput)
            

 

import os

def main():
    while True:
        user_input = input("Input filename\n")
        filename = user_input + ".txt"
        if os.path.exists(filename):
            with open(filename, 'r') as f:
                contents = f.read()
                print(contents)
                break

if __name__ == '__main__':
    main()

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, 79wjd said:

import os

def main():
    while True:
        user_input = input("Input filename\n")
        filename = user_input + ".txt"
        if os.path.exists(filename):
            break
    with open(filename, 'r') as f:
        contents = f.read()
        print(contents)

if __name__ == '__main__':
	main()

What is this. What does it do. Please could you talk and explain to me. Thanks

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, 79wjd said:

import os

def main():
    while True:
        user_input = input("Input filename\n")
        filename = user_input + ".txt"
        if os.path.exists(filename):
            with open(filename, 'r') as f:
                contents = f.read()
                print(contents)
                break

if __name__ == '__main__':
    main()

It still gives and error

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Safeer Hafiz said:

What is this. What does it do. Please could you talk and explain to me. Thanks

When you run the program, it will go into the main function, prompt the user for input, if the input is a valid file then it will open and print the file. Otherwise will it re-prompt the user for input.

1 minute ago, Safeer Hafiz said:

It still gives and error

 

What error?

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, 79wjd said:

What error?

So when i pasted the code you made. It gave me an error

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Safeer Hafiz said:

So when i pasted the code you made. It gave me an error

What is the error?

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

unexpected indentation. Plus is there a way to just keep my code and not change it all.

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, 79wjd said:

What is the error?

unexpected indentation

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Safeer Hafiz said:

unexpected indentation.

You need to fix the indentation somewhere. It tells you what line has the wrong indentation. Just correct it. 

Just now, Safeer Hafiz said:

Plus is there a way to just keep my code and not change it all.

Considering your code wasn't valid, no. There are many ways to do it, but the way you originally had it was wrong.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, 79wjd said:

You need to fix the indentation somewhere. It tells you what line has the wrong indentation. Just correct it. 

Considering your code wasn't valid, no. There are many ways to do it, but the way you originally had it was wrong.

But it was valid. Everything worked. Its just that if the user imputed a file that didn't exist it would just display an error. 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Safeer Hafiz said:

But it was valid. Everything worked. Its just that if the user imputed a file that didn't exist it would just display an error. 

You need to add a while loop. If you really wanted then you could put the while loop (with the input request) outside of main (and all functions) like you had, but that's a really horrible coding practice.

 

import os

while True:

    user_input = input("Input filename\n")

    if os.path.exists(user_input):

        filename = user_input + ".txt"

        break

def main():

    f = open(filename, 'r')

    contents = f.read()

    print(contents)

    f.close()

if __name__ == '__main__':

    main()

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

54 minutes ago, 79wjd said:

You need to add a while loop. If you really wanted then you could put the while loop (with the input request) outside of main (and all functions) like you had, but that's a really horrible coding practice.

 


import os

while True:

    user_input = input("Input filename\n")

    if os.path.exists(user_input):

        filename = user_input + ".txt"

        break

def main():

    f = open(filename, 'r')

    contents = f.read()

    print(contents)

    f.close()

if __name__ == '__main__':

    main()

Its not like i want it to look professional. I just want the program to work in the simplest way. 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

56 minutes ago, 79wjd said:

You need to add a while loop. If you really wanted then you could put the while loop (with the input request) outside of main (and all functions) like you had, but that's a really horrible coding practice.

 


import os

while True:

    user_input = input("Input filename\n")

    if os.path.exists(user_input):

        filename = user_input + ".txt"

        break

def main():

    f = open(filename, 'r')

    contents = f.read()

    print(contents)

    f.close()

if __name__ == '__main__':

    main()

Ok so the program runs now but when i enter the file name it just says input file name again. And keeps saying input file name

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Safeer Hafiz said:

Ok so the program runs now but when i enter the file name it just says input file name again. And keeps saying input file name

 

Then

if os.path.exists(filename)

is never evaluating to true. Are you sure you're entering the filename correctly?

 

If the directory looks like:

  • Dir
    • script.py
    • file.txt

and you're running it from within Dir. (e.g. `python script.py`), then you would need to enter `file`. If you were running it from a different directory (e.g. `python PATH_TO_SCRIPT/script.py`), then you would need to enter 'PATH_TO_FILE/file`.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, 79wjd said:

Then

 


if os.path.exists(filename)

 

is never evaluating to true. Are you sure you're entering the filename correctly?

 

If the directory looks like:

  • Dir
    • script.py
    • file.txt

and you're running it from within Dir. (e.g. `python script.py`), then you would need to enter `file`. If you were running it from a different directory (e.g. `python PATH_TO_SCRIPT/script.py`), then you would need to enter 'PATH_TO_FILE/file`.

i have entered correct name of file. Still does not work

 

CPU: Ryzen 5 2600X | Motherboard: MSI B450M Mortar (Black) RAM: Corsair Vengeance LPX 16GB (2x 8GB) 3000MHz DDR4 | GPU: MSI Radeon RX 580 ARMOR 8GB | Storage: Samsung 860 QVO 1TB and Samsung 970 EVO 250GB | PSU: Corsair CX650M 650W Case: Cooler Master MasterBox Q300L | Monitor: ASUS VZ279HE | Mouse: Logitech M171 Wireless | Keyboard: Cherry DC 2000 | Mousepad: None | Speakers: Logitech Z333

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Safeer Hafiz said:

i have entered correct name of file. Still does not work

 

Make it 


filename = user_input + '.txt'

if os.path.exists(filename):

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×