From 84e066839cef53d73067ed7bf90627727027017a Mon Sep 17 00:00:00 2001 From: 645891bf0975d14e43ddbf8e <645891bf0975d14e43ddbf8e@devide> Date: Tue, 9 May 2023 02:42:17 +0000 Subject: [PATCH] Auto commit --- __pycache__/main.cpython-38.pyc | Bin 0 -> 1694 bytes main.py | 70 ++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 __pycache__/main.cpython-38.pyc diff --git a/__pycache__/main.cpython-38.pyc b/__pycache__/main.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f5be08da58d1ef75d08baad58a0ef95ded190caf GIT binary patch literal 1694 zcmb_c&2Jk;6rb7eI8GXh1X2&kpjN^M92TLJySjc(K4(g39+m5JYf{{ItbB2HS{@y6KzvWS`|*8f*KuZd2@Z ztQq6F<2x|MBK9WoUBEc8S9g6EqYH?*#M?x~qxjNH*Yq=Vmdw-)wncHz>m!UN_U_as z_#(<@#t2I^p8 z5UYzjM%(C_eZ-c8)i%1&S3`WN8#Rl)Cv0MOF-EHkDWZ*PhO(^HzBX$aw|Lgfh)@+K!D@|78t*}BD>tWO=w64mmh6U-^S%C60AP~-C%kZ#= zb2tOKt_Jupdj@**X>)=*YLpFi2`YaT1V!os_y#ozrq(5!7~4uWyBH*}hX4yU;f&Si zJH&-E_T;G#C>Ix0fb#pSAhrB#K4U z;03)=6-DJ~QK3De_M2c3+%^KZp9F#-cGYU*0BGGs9ZQ&PvuE`+{o1H!rxtPghPuTN zrduAX1SKv?vs5feGhD7n32OwG!#JUGmPdpJ zbY+EBgg#xSXdt~R6%#s_wKGpppzsg8oOh)c1dXsxgFt42pxz`&tn_RUsPNJr zW~gP1s42aR0&Yk3-S^pxCj(KJf~jcOX%IQ6+ymhO#~t{zS1qNkNf6|-4Bmd&lcW&| z6^@*nY6X?8UXYzr5EJ;y)H|xT65d#tKzb^r^C9e_T8*i!AKR2iu}JT&G@-ZE`^H}Y Xp{`&SDt!7ng8ppQI6Vip?cu)x-t03$ literal 0 HcmV?d00001 diff --git a/main.py b/main.py index 721e7d4..039a4a6 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,55 @@ import random +import unittest +from unittest.mock import patch +import random +import io +import sys + +import unittest +import random +from unittest import mock +def guess_number(): + number = random.randint(1, 100) + count = 0 + while True: + count += 1 + guess = input("请输入一个 1-100 之间的整数:") + if not guess.isdigit() or int(guess) < 1 or int(guess) > 100: + print("输入不合法,请重新输入!") + elif int(guess) < number: + print("猜的数字太小了,再试试!") + elif int(guess) > number: + print("猜的数字太大了,再试试!") + else: + print("恭喜你,猜对了!你总共猜了 %d 次。" % count) + break + return count + + + +from main import guess_number + +class TestGuessNumber(unittest.TestCase): + + @patch('sys.stdout', new_callable=io.StringIO) + @patch('random.randint') + @patch('builtins.input') + def test_input(self, mock_input, mock_randint, mock_stdout): + mock_randint.return_value = 50 # 将随机数固定为50 + mock_input.side_effect = ['0', '101', 'abc', '50'] + count = guess_number() + print(mock_stdout.getvalue()) + self.assertEqual(mock_stdout.getvalue(), '输入不合法,请重新输入!\n输入不合法,请重新输入!\n输入不合法,请重新输入!\n恭喜你,猜对了!你总共猜了 4 次。\n') + #self.assertEqual(count, 1) + +if __name__ == '__main__': + unittest.main() + + + + +if __name__ == '__main__': + unittest.main() + + -# 生成一个随机数 -number = random.randint(1, 100) - -# 记录猜的次数 -count = 0 - -# 循环猜数字 -while True: - guess = int(input("请猜一个1到100之间的数字:")) - count += 1 - if guess == number: - print("恭喜你猜对了!你一共猜了%d次。" % count) - break - elif guess < number: - print("猜的数字太小了,请再猜一次。") - else: - print("猜的数字太大了,请再猜一次。") -- GitLab