How to compare two two-digit integers, as, on the contrary, and one character at a time

I am new to programming and try to learn it by taking intro class using python.

One of my tasks requires us to do the following:

  • compare a randomly generated two-digit integer with the integer generated by the user.

    • if both random and user-created integers match print blah1
    • if the user-generated integer has the same two digits as the randomly generated integer in the reverse order, print blah2
    • if the user-generated integer has one digit that matches the randomly generated integer, print blah3.

Now we just studied the basic material (operators, if / else / elif , while loops, printing, lines, integers)

I came up with something that randomly assigns two digits, converts them into strings, and then combines them into a two-digit string. From here, I used elif expressions to match every possible condition.

Unfortunately, this is not what is required. I have to use two integers when making comparisons. Unfortunately, I have absolutely no idea how to compare parts of an integer or cancel an integer with what I was taught.

Now I'm not looking for someone to solve this for me. I need help, either a hint or a suggestion on how I should think about it with the basic knowledge that I have.

Any help is appreciated.

I included the code I wrote.

 # homework 2 # problem 1 # # lottery guessing program # # the program randomly generates a two-digit number, prompts the user to enter a two-digit number, # and determines whether the user wins according to the following rules: # # 1. if both digits match in the right order, you will win $10,000. # 2. if both digits match, but in the reversed order, you will win $3,000. # 3. if you match one digit in either place, you will win $1,000 #imports import random #rules print("guess a number. if both digits match in the right order, you will win $10,000."\ "\nif both digits match, but in the reversed order, you will win $3,000." \ "\nif you match one digit in either place, you will win $1,000." \ "\nif you don't match any digits, you do not win anything.") # random variables rand_num1 = str(random.randint(1,9)) rand_num2 = str(random.randint(1,9)) #ask user for number user_num1 = input("what is your first number? ") user_num2 = input("what is your second number? ") #for testing purposes, if testing, comment out the previous two lines #combd_num = (str(rand_num1)) + (str(rand_num2)) #revsd_num = (str(rand_num2)) + (str(rand_num1)) #print(rand_num1) #print(rand_num2) #print(combd_num) #print(revsd_num) #user_num1 = input("what is your first number? ") #user_num2 = input("what is your second number? ") #ucomb_num = (str(user_num1)) + (str(user_num2)) #output the numbers print("the number is, ", (rand_num1 + rand_num2),"\ \nyour number is, ", (user_num1 + user_num2), sep="") #elif statement if (user_num1 + user_num2) == (rand_num1 + rand_num2): print("you guessed the exact number. you win $10,000!") elif (user_num2 + user_num1) == (rand_num1 + rand_num2): print("you guessed both digits but in reverse. you win $3,000!") elif user_num1 == rand_num1 and user_num2 != rand_num2: print("you guessed one digit right. you win $1,000!") elif user_num1 == rand_num2 and user_num2 != rand_num2: print("you guessed one digit right. you win $1,000!") elif user_num2 == rand_num1 and user_num1 != rand_num2: print("you guessed one digit right. you win $1,000!") elif user_num2 == rand_num2 and user_num1 != rand_num2: print("you guessed one digit right. you win $1,000!") else: print("sorry, you didn't guess the right number.") 
+4
source share
3 answers

There are three tricks to help you here:

  • Lists can be compared in the same way as strings and numbers. The list of integers can be compared with another, and if the contained numbers match, the comparison returns True :

     >>> [1, 2] == [1, 2] True >>> [1, 3] == [1, 2] False 
  • You can easily flip the list. You can use the built-in function reversed() , or you can use the notation of the [start:stop:stride] fragment to give a negative step. The latter also gives you an inverted list:

     >>> list(reversed([1, 2])) [2, 1] >>> [1, 2][::-1] [2, 1] 

    The reversed() function returns an iterator, passing it to the list() constructor, we get the list again.

  • You can use the in operator to verify membership in a list. Use this to check if an individual integer is part of a list of integers:

     >>> 1 in [1, 2] True >>> 3 in [1, 2] False 

These three tricks together should provide you with all the tools you need to reprogram your script to work with integers. Store both your random numbers and user input (converted to integers with int() function) in lists and work from there.

If you have to accept a single integer input between 10 and 99, this is a little trickier. You can highlight 10 and 1 using the modulo % and division \ operators:

 ones = number % 10 tens = number // 10 

You can combine two operations using the divmod() function:

 tens, ones = divmod(number, 10) 

You now have two separate integers to perform your comparisons.

+4
source

I think that you are faced with the task of obtaining individual digits from a two-digit integer. This can be done using mathematical operators.

Just think about what each digit represents in an amount like 42 . 4 is the number β€œtens”, and 2 is the β€œone” figure. This may help to think about it in reverse order. If you have integers 4 and a 2 in separate variables, how would you put them together into 42 ?

Hope my tips help you! If I were too oblique, I could try to develop a bit more, but I want you to be able to solve it yourself, and I suspect you will get it quickly as soon as you think about the right operators to use.

0
source

Ok, thanks everyone. because of your suggestions and hints, I was able to complete the task in accordance with the specifications.

while I'm not too proud of how this was done - it just seems messy for me - it works.

here he is:

 #imports import random #rules print("guess a number. if both digits match in the right order, you will win $10,000."\ "\nif both digits match, but in the reversed order, you will win $3,000." \ "\nif you match one digit in either place, you will win $1,000." \ "\nif you don't match any digits, you do not win anything.") #random variable rand_num = random.randint(0,99) print(rand_num) #separate the digits rand_num_tens = rand_num//10 print(rand_num_tens) rand_num_ones = rand_num%10 print(rand_num_ones) #reverse the random number digits revsd_rnd_num = (rand_num_ones * 10) + rand_num_tens print(revsd_rnd_num) #ask user for number user_num = int(input("guess a number between 1 and 99: ")) #separate the digits user_num_tens = user_num//10 print(user_num_tens) user_num_ones = user_num%10 print(user_num_ones) #output the numbers print("the number is, ", rand_num,"\ \nyour number is, ", user_num, sep="") #elif statement if rand_num == user_num: print("you guessed the exact number. you win $10,000!") elif revsd_rnd_num == user_num: print("you guessed both digits but in reverse. you win $3,000!") elif rand_num_tens == user_num_tens: print("you guessed one digit right. you win $1,000!") elif rand_num_tens == user_num_ones: print("you guessed one digit right. you win $1,000!") elif rand_num_ones == user_num_tens: print("you guessed one digit right. you win $1,000!") elif rand_num_ones == user_num_ones: print("you guessed one digit right. you win $1,000!") else: print("sorry, you didn't guess the right number.") 
0
source

Source: https://habr.com/ru/post/1436534/


All Articles