Python 如何按原样比较两个两位数整数、反向整数和一次比较一个字符

Python 如何按原样比较两个两位数整数、反向整数和一次比较一个字符,python,string,python-3.x,integer,logic,Python,String,Python 3.x,Integer,Logic,我是编程新手,正在尝试通过使用python学习入门课程来学习编程 我的一项任务要求我们做以下工作: 将随机生成的两位数整数与用户生成的两位数整数进行比较 如果随机整数和用户生成的整数都匹配,则打印blah1 如果用户生成的整数与随机生成的整数的两位数相反,则打印blah2 如果用户生成的整数有一个数字与随机生成的整数相同,请打印blah3 现在,到目前为止,我们只学习了基本的东西,(操作符,if/else/elif,while循环、打印、字符串、整数) 我想出了一种方法,随机分配两个数字,将它

我是编程新手,正在尝试通过使用python学习入门课程来学习编程

我的一项任务要求我们做以下工作:

  • 将随机生成的两位数整数与用户生成的两位数整数进行比较

  • 如果随机整数和用户生成的整数都匹配,则打印blah1
  • 如果用户生成的整数与随机生成的整数的两位数相反,则打印blah2
  • 如果用户生成的整数有一个数字与随机生成的整数相同,请打印blah3
  • 现在,到目前为止,我们只学习了基本的东西,(操作符,
    if
    /
    else
    /
    elif
    while
    循环、打印、字符串、整数)

    我想出了一种方法,随机分配两个数字,将它们转换成字符串,然后将它们连接成一个两位数的字符串。从这里开始,我使用
    elif
    语句来匹配每个可能的条件

    不幸的是,这不是必需的。当我进行比较时,我必须使用两位整数。不幸的是,我完全不知道如何比较整数的各个部分,或者如何将整数与我所学的进行反转

    现在,我不是在找人帮我解决这个问题。我需要一些帮助,或者是一个提示,或者是一个建议,告诉我应该如何用我的基本知识来思考这个问题

    非常感谢您的任何帮助

    我已经包括了我写的代码

    # 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.")
    

    我认为您面临的挑战是如何从两位整数中获取独立的数字。使用数学运算符是可能的

    想想像
    42
    这样的数字中的每个数字代表什么。
    4
    是“十”位,2是“一”位。反过来考虑可能会有所帮助。如果整数
    4
    2
    在单独的变量中,如何将它们组合成单个整数
    42


    希望我的提示能帮到你!如果我说得太委婉了,我可以试着详细阐述一下,但我确实希望您能够自己解决这个问题,我想一旦您想到了正确的运算符,您会很快得到它。

    这里有三个技巧可以帮助您:

  • 列表可以像字符串和数字一样进行比较。整数列表可以与另一个整数进行比较,如果包含的数字相同,则比较返回
    True

    >>> [1, 2] == [1, 2]
    True
    >>> [1, 3] == [1, 2]
    False
    
  • 你可以很容易地撤销列表。您可以使用
    reversed()
    内置函数,也可以使用
    [开始:停止:步幅]
    切片表示法给出负步幅。后者也为您提供了一个相反的列表:

    >>> list(reversed([1, 2]))
    [2, 1]
    >>> [1, 2][::-1]
    [2, 1]
    
    reversed()
    函数返回一个迭代器,通过将迭代器传递给
    list()
    构造函数,我们再次得到一个列表

  • 您可以使用中的
    操作符测试列表成员资格。使用此选项测试单个整数是否为整数列表的一部分:

    >>> 1 in [1, 2]
    True
    >>> 3 in [1, 2]
    False
    
  • 这三个技巧结合在一起,将为您提供重新编程脚本以使用整数所需的所有工具。将您的随机数和用户输入(使用
    int()
    函数转换为整数)存储在列表中,然后从中开始工作

    如果您必须接受10到99之间的一个整数输入,那么事情就变得有点棘手了。您可以使用th模
    %
    和除法
    \
    运算符将10和1分开:

    ones = number % 10
    tens = number // 10
    
    您可以使用以下命令组合这两个操作:


    现在您又有了两个单独的整数来进行比较。

    好的,感谢大家的帮助。由于你的建议和提示,我按照规范完成了作业

    然而,我并不为它是如何完成的而感到骄傲——只是在我看来有些草率——它是有效的

    这是:

    #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.")
    

    Sameer还可能会发现知道明显的
    [1,2]==reversed([2,1])
    给出了
    False
    ,这很有帮助,您必须使用
    [1,2]==list(reversed([2,1])
    或切片符号来获取列表进行比较。顺便说一句,我不相信您使用
    reversed()
    给出的示例的输出。对了,在Python3中,reversed是一个迭代器,我仍然经常使用Python2。我会更正的。Python 2中的
    reversed
    是否也返回
    listreverseiterator对象
    ?@MartijnPieters:我不知道为什么以前没有注意到整数除法和模运算符。谢谢,差不多做到了。
    #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.")