-4

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

Park
  • 2,446
  • 1
  • 16
  • 25
  • You should look at the answers to this [question](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) There are lots of answers which try to validate input. – quamrana Jan 22 '22 at 13:07
  • https://www.regular-expressions.info – deceze Jan 22 '22 at 13:08

3 Answers3

0

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)
XxJames07-
  • 1,833
  • 1
  • 4
  • 17
-1

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")
Styx1337
  • 37
  • 6
-2
user_input = str("Type your blub User")
if user_input == "blub100k:"
   print("Okay")
else:
   print("Not Okay")
DrFloyd5
  • 13,555
  • 3
  • 25
  • 34
akul07
  • 27
  • 1
  • 7
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 23 '22 at 01:07