python - 如何在我的问题中定义更多选项?
问题描述
a1=str(input("4.Name one neighbouring country of India."))
if a1.lower()== ("pakistan"):
print("correct +20 Score")
score+=20
if a1.lower()== ("china"):
print("correct +20 Score")
score+=20
if a1.lower()== ("nepal"):
print("correct +20 Score")
score+=20
if a1.lower()== ("bhutan"):
print("correct +20 Score")
score+=20
if a1.lower()== ("bangladesh"):
print("correct +20 Score")
score+=20
if a1.lower()== ("myanmar"):
print("correct +20 Score")
score+=20
if a1.lower()== ("sri lanka"):
print("correct +20 Score")
score+=20
if a1.lower()== ("maldivs"):
print("correct +20 Score")
score+=20
else:
print("incorrect +0 Score")
我这样做是因为我的问题包含 8 个答案,但在输出中,它同时打印“正确 +20 分数”和“不正确 +0 分数”。我想修复它请帮助我。
解决方案
比较时需要使用else
语句,而不是多个ifs
. 如果你需要多个ifs
,你应该使用elif
.
https://www.tutorialspoint.com/python/python_if_else.htm
但是,将您的答案存储在列表中并检查用户的响应是否存在于列表中会更加清晰和简洁,否则没有分数。
neighbouring = ['pakistan', 'china', 'nepal', 'bhutan', 'bangladesh', 'myanmar', 'sri lanka', 'maldivs']
a1=str(input("4.Name one neighbouring country of India."))
if a1.lower() in neighbouring:
print('correct 20 score')
score += 20
else:
print('incorrect')
推荐阅读
- unit-testing - 错误:Nil 的未定义方法“x”(编译时类型为 (Point | Nil))
- node.js - 使用“npm init”命令制作 package.json 时出错
- sql - 基于 SELECT 脚本创建 DELETE 脚本
- magento - Magento 2.3.3 客户送货地址名麻烦。“名字”是必需的
- node.js - 在 Ubuntu 服务器上解决 net::ERR_TUNNEL_CONNECTION_FAILED
- android - Android:如果他想使用导航意图,如何向用户显示应用选择器?
- java - 如何在 Spring Boot 2.2 中添加soap 标头以调用soap 端点?
- android - 如何检测 system_server 中的内存泄漏
- arduino - 使用 arduino IDE 的 ESP32 中的 BLE 连接白名单
- aspnetboilerplate - 域服务未在 AppService 中注册