我的石头剪刀布Python程序在Visual Studio Code中无法运行求助
石头剪刀布代码在VSCode中无响应的解决方法
你遇到的问题并不是代码逻辑错误,而是程序在等待用户输入时被忽略了。
问题现象
运行程序后终端仅显示启动命令:
& C:/Users/user/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/user/New folder/main.py"
原因
代码中的input()语句会暂停程序执行,等待你在终端输入内容,但很多人容易没注意到终端的输入提示,误以为程序没运行。
解决步骤
- 运行程序后,切换到VSCode的终端窗口
- 在光标位置输入
rock/paper/scissors(或输入q退出),按下回车就能看到程序输出结果
优化后的代码(中文提示+更规范的退出逻辑)
import random is_playing = True choices = ['rock', 'paper', 'scissors'] while is_playing: computer_choice = random.choice(choices) player_choice = input('石头、剪刀、布?输入q退出游戏\n').lower() if player_choice == computer_choice: print('平局!') elif player_choice == 'rock': if computer_choice == 'scissors': print(f'你选了{player_choice},电脑选了{computer_choice}!你赢了!') else: print(f'你选了{player_choice},电脑选了{computer_choice}!你输了!') elif player_choice == 'paper': if computer_choice == 'rock': print(f'你选了{player_choice},电脑选了{computer_choice}!你赢了!') else: print(f'你选了{player_choice},电脑选了{computer_choice}!你输了!') elif player_choice == 'scissors': if computer_choice == 'paper': print(f'你选了{player_choice},电脑选了{computer_choice}!你赢了!') else: print(f'你选了{player_choice},电脑选了{computer_choice}!你输了!') elif player_choice == 'q': is_playing = False print('游戏结束') else: print('输入无效,请输入rock、paper、scissors或q')
内容的提问来源于stack exchange,提问作者Yuvi5000
相关产品推荐
相关产品推荐

