python - 如何按原样、反转和一次比较一个字符来比较两个两位数整数

标签 python string python-3.x integer logic

我是编程新手,正在尝试通过使用 Python 参加入门类(class)来学习它。

我的一项任务要求我们执行以下操作:

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

    1. 如果随机数和用户生成的整数都匹配 print blah1
    2. 如果用户生成的整数与随机生成的整数的两位数相同,则打印blah2
    3. 如果用户生成的整数有一位与随机生成的整数相同,则打印 blah3。

到目前为止,我们只学习了基本的东西,(运算符,if/else/elifwhile 循环、打印、字符串、整数)

我想出了一个随机分配两个数字的方法,将它们转换成字符串,然后将它们连接成一个两位数的字符串。从这里开始,我使用 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.")

最佳答案

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

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

    >>> [1, 2] == [1, 2]
    True
    >>> [1, 3] == [1, 2]
    False
    
  2. 您可以轻松反转列表。您可以使用 reversed() 内置函数,也可以使用 [start:stop:stride] 切片符号来给出一个负数 大步前进。后者也为您提供了一个反向列表:

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

    reversed() 函数返回一个迭代器,通过将其传递给 list() 构造函数,我们再次获得一个列表。

  3. 您可以使用 in 运算符来测试列表成员资格。使用它来测试单个整数是否是整数列表的一部分:

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

这 3 个技巧应该可以为您提供重新编程脚本以使用整数所需的所有工具。将您的随机数和用户输入(使用 int() 函数转换为整数)存储在列表中并从那里开始工作。

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

ones = number % 10
tens = number // 10

您可以使用 divmod() function 组合这两个操作:

tens, ones = divmod(number, 10)

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

关于python - 如何按原样、反转和一次比较一个字符来比较两个两位数整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12616074/

相关文章:

c - 该程序打印不需要的东西

python - pickle Spark RDD 并将其读入 Python

python - 使用 dict() 在循环中返回多个变量

Python 运算符 pymysql

python:按名称分配属性值

python - 如何在给定代码中运行 if 循环

python-3.x - 如何绘制分类特征的 Cramer’s V 热图?

python - 操作系统错误 : [Errno 18] Invalid cross-device link

json - 如何使用 String() 方法将嵌入式结构正确序列化为 JSON 字符串?

php - 从字符串中返回所有匹配的 css