Home > front end >  python check if user input has the correct int value in a string
python check if user input has the correct int value in a string

Time:01-23

I try to fix a problem where the user can input for example "blub1234" or "blub4567" or "blub912" and the code can check if the user typed in the right word "blub" and the possible value as an int from 1 up to 100k.

user_input = str("Type your blub User)
if user_input == ("user_input"   ???):
   print("Okay")
else:
   print("Not Okay")

The user shouldn't type for example "blob2312" or "blib1212" only the word "blub" with the number value to 100k. I tried it with ranges and for loops, but it ends every time with:

TypeError: can only concatenate str (not "int") to str

CodePudding user response:

If you know both the string and the int and just want to check if the input is equal to that fixed combination you can do:

pw_int = 1234
pw_str = "blob"

    if user_input == (pw_str   str(pw_int)):
        print(f"PW was {pw_str}{pw_int} -> Okay")
    else:
        print("Not Okay")

CodePudding user response:

you could use regex:

import re
String_Attached_To_String = 123
user_input = input("String")
re.findall(r"{}(\d )".format(String_Attached_To_String),user_input)

CodePudding user response:

user_input = str("Type your blub User")
if user_input == "blub100k:
   print("Okay")
else:
   print("Not Okay")
  •  Tags:  
  • Related