Community

개발자 99% 커뮤니티에서 수다 떨어요!

← Go back
while 문 안에서 try except 사용시 except를 거치면 조건이 되도 while이 안 꺼져요
#python
2년 전
1,111
2

python while 문 안에 try except문을 사용했는데 except를 거치면 while이 끝나는 조건이 되어도 끝나지가 않고 3번 더 실행되야 끝나네요 ㅠㅠ 꼭 알고싶습니다 도와주세요

코드

import os

import requests

from bs4 import BeautifulSoup

os.system("clear")

url = "https://www.iban.com/currency-codes"

country_dict = {}

currency_dict = {}

def extract():

result = requests.get("https://www.iban.com/currency-codes")

soup = BeautifulSoup(result.text, "html.parser")

table = soup.find("table", {"class" : "table table-bordered downloads tablesorter"})

tr = table.find_all("tr")

del tr[0]

print("Hello! Please choose select a country by number:")

for x in tr:

td = x.find_all("td")

country1 = str(td[0]).replace("<td>", "")

country = country1.replace("</td>", "")

currency1 = str(td[1]).replace("<td>", "")

currency = currency1.replace("</td>", "")

code1 = str(td[2]).replace("<td>", "")

code = code1.replace("</td>", "")

number1 = str(td[3]).replace("<td>", "")

number = number1.replace("</td>", "")

try:

country_dict[int(number)] = country

currency_dict[country] = currency

print("#", number, country)

except:

country_dict[f"None_num_{country}"] = country

currency_dict[country] = currency

print("#", f"None_num_({country})", country)

def finding ():

A=0

while A==0:

try:

i=input("# : ")

num_find = int(i)

if type(num_find) is int and (num_find in country_dict) is False:

print("Choose a number from the list")

A=0

elif type(num_find) is int and (num_find in country_dict) is True:

print(f"You choose {country_dict[num_find]}\nThe currency code is {currency_dict[(country_dict[num_find])]}")

A=1

except:

print("That wasn't a number")

finding()

extract()

finding()

---------------------------------------------------------------------------------

실행결과

Hello! Please choose select a country by number:

# 971 AFGHANISTAN

# 978 ÅLAND ISLANDS

# 008 ALBANIA

# 012 ALGERIA

# 840 AMERICAN SAMOA

# 978 ANDORRA

# 973 ANGOLA

# 951 ANGUILLA

# None_num_(ANTARCTICA) ANTARCTICA

# 951 ANTIGUA AND BARBUDA

# 032 ARGENTINA

# 051 ARMENIA

# 533 ARUBA

# 036 AUSTRALIA

# 978 AUSTRIA

# 944 AZERBAIJAN

# 044 BAHAMAS (THE)

# 048 BAHRAIN

# 050 BANGLADESH

# 052 BARBADOS

# 933 BELARUS

# 978 BELGIUM

# 084 BELIZE

# 952 BENIN

# 060 BERMUDA

# 064 BHUTAN

# 356 BHUTAN

# 068 BOLIVIA (PLURINATIONAL STATE OF)

# 984 BOLIVIA (PLURINATIONAL STATE OF)

# 840 BONAIRE, SINT EUSTATIUS AND SABA

# 977 BOSNIA AND HERZEGOVINA

# 072 BOTSWANA

# 578 BOUVET ISLAND

# 986 BRAZIL

# 840 BRITISH INDIAN OCEAN TERRITORY (THE)

# 096 BRUNEI DARUSSALAM

# 975 BULGARIA

# 952 BURKINA FASO

# 108 BURUNDI

# 132 CABO VERDE

# 116 CAMBODIA

# 950 CAMEROON

# 124 CANADA

# 136 CAYMAN ISLANDS (THE)

# 950 CENTRAL AFRICAN REPUBLIC (THE)

# 950 CHAD

# 990 CHILE

# 152 CHILE

# 156 CHINA

# 036 CHRISTMAS ISLAND

# 036 COCOS (KEELING) ISLANDS (THE)

# 170 COLOMBIA

# 970 COLOMBIA

# 174 COMOROS (THE)

# 976 CONGO (THE DEMOCRATIC REPUBLIC OF THE)

# 950 CONGO (THE)

# 554 COOK ISLANDS (THE)

# 188 COSTA RICA

# 952 CÔTE D'IVOIRE

# 191 CROATIA

# 931 CUBA

# 192 CUBA

# 532 CURAÇAO

# 978 CYPRUS

# 203 CZECH REPUBLIC (THE)

# 208 DENMARK

# 262 DJIBOUTI

# 951 DOMINICA

# 214 DOMINICAN REPUBLIC (THE)

# 840 ECUADOR

# 818 EGYPT

# 222 EL SALVADOR

# 840 EL SALVADOR

# 950 EQUATORIAL GUINEA

# 232 ERITREA

# 978 ESTONIA

# 230 ETHIOPIA

# 978 EUROPEAN UNION

# 238 FALKLAND ISLANDS (THE) [MALVINAS]

# 208 FAROE ISLANDS (THE)

# 242 FIJI

# 978 FINLAND

# 978 FRANCE

# 978 FRENCH GUIANA

# 953 FRENCH POLYNESIA

# 978 FRENCH SOUTHERN TERRITORIES (THE)

# 950 GABON

# 270 GAMBIA (THE)

# 981 GEORGIA

# 978 GERMANY

# 936 GHANA

# 292 GIBRALTAR

# 978 GREECE

# 208 GREENLAND

# 951 GRENADA

# 978 GUADELOUPE

# 840 GUAM

# 320 GUATEMALA

# 826 GUERNSEY

# 324 GUINEA

# 952 GUINEA-BISSAU

# 328 GUYANA

# 332 HAITI

# 840 HAITI

# 036 HEARD ISLAND AND McDONALD ISLANDS

# 978 HOLY SEE (THE)

# 340 HONDURAS

# 344 HONG KONG

# 348 HUNGARY

# 352 ICELAND

# 356 INDIA

# 360 INDONESIA

# 960 INTERNATIONAL MONETARY FUND (IMF)

# 364 IRAN (ISLAMIC REPUBLIC OF)

# 368 IRAQ

# 978 IRELAND

# 826 ISLE OF MAN

# 376 ISRAEL

# 978 ITALY

# 388 JAMAICA

# 392 JAPAN

# 826 JERSEY

# 400 JORDAN

# 398 KAZAKHSTAN

# 404 KENYA

# 036 KIRIBATI

# 408 KOREA (THE DEMOCRATIC PEOPLE’S REPUBLIC OF)

# 410 KOREA (THE REPUBLIC OF)

# 414 KUWAIT

# 417 KYRGYZSTAN

# 418 LAO PEOPLE’S DEMOCRATIC REPUBLIC (THE)

# 978 LATVIA

# 422 LEBANON

# 426 LESOTHO

# 710 LESOTHO

# 430 LIBERIA

# 434 LIBYA

# 756 LIECHTENSTEIN

# 978 LITHUANIA

# 978 LUXEMBOURG

# 446 MACAO

# 807 REPUBLIC OF NORTH MACEDONIA

# 969 MADAGASCAR

# 454 MALAWI

# 458 MALAYSIA

# 462 MALDIVES

# 952 MALI

# 978 MALTA

# 840 MARSHALL ISLANDS (THE)

# 978 MARTINIQUE

# 929 MAURITANIA

# 480 MAURITIUS

# 978 MAYOTTE

# 965 MEMBER COUNTRIES OF THE AFRICAN DEVELOPMENT BANK GROUP

# 484 MEXICO

# 979 MEXICO

# 840 MICRONESIA (FEDERATED STATES OF)

# 498 MOLDOVA (THE REPUBLIC OF)

# 978 MONACO

# 496 MONGOLIA

# 978 MONTENEGRO

# 951 MONTSERRAT

# 504 MOROCCO

# 943 MOZAMBIQUE

# 104 MYANMAR

# 516 NAMIBIA

# 710 NAMIBIA

# 036 NAURU

# 524 NEPAL

# 978 NETHERLANDS (THE)

# 953 NEW CALEDONIA

# 554 NEW ZEALAND

# 558 NICARAGUA

# 952 NIGER (THE)

# 566 NIGERIA

# 554 NIUE

# 036 NORFOLK ISLAND

# 840 NORTHERN MARIANA ISLANDS (THE)

# 578 NORWAY

# 512 OMAN

# 586 PAKISTAN

# 840 PALAU

# None_num_(PALESTINE, STATE OF) PALESTINE, STATE OF

# 590 PANAMA

# 840 PANAMA

# 598 PAPUA NEW GUINEA

# 600 PARAGUAY

# 604 PERU

# 608 PHILIPPINES (THE)

# 554 PITCAIRN

# 985 POLAND

# 978 PORTUGAL

# 840 PUERTO RICO

# 634 QATAR

# 978 RÉUNION

# 946 ROMANIA

# 643 RUSSIAN FEDERATION (THE)

# 646 RWANDA

# 978 SAINT BARTHÉLEMY

# 654 SAINT HELENA, ASCENSION AND TRISTAN DA CUNHA

# 951 SAINT KITTS AND NEVIS

# 951 SAINT LUCIA

# 978 SAINT MARTIN (FRENCH PART)

# 978 SAINT PIERRE AND MIQUELON

# 951 SAINT VINCENT AND THE GRENADINES

# 882 SAMOA

# 978 SAN MARINO

# 930 SAO TOME AND PRINCIPE

# 682 SAUDI ARABIA

# 952 SENEGAL

# 941 SERBIA

# 690 SEYCHELLES

# 694 SIERRA LEONE

# 702 SINGAPORE

# 532 SINT MAARTEN (DUTCH PART)

# 994 SISTEMA UNITARIO DE COMPENSACION REGIONAL DE PAGOS "SUCRE"

# 978 SLOVAKIA

# 978 SLOVENIA

# 090 SOLOMON ISLANDS

# 706 SOMALIA

# 710 SOUTH AFRICA

# None_num_(SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS) SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS

# 728 SOUTH SUDAN

# 978 SPAIN

# 144 SRI LANKA

# 938 SUDAN (THE)

# 968 SURINAME

# 578 SVALBARD AND JAN MAYEN

# 748 SWAZILAND

# 752 SWEDEN

# 947 SWITZERLAND

# 756 SWITZERLAND

# 948 SWITZERLAND

# 760 SYRIAN ARAB REPUBLIC

# 901 TAIWAN (PROVINCE OF CHINA)

# 972 TAJIKISTAN

# 834 TANZANIA, UNITED REPUBLIC OF

# 764 THAILAND

# 840 TIMOR-LESTE

# 952 TOGO

# 554 TOKELAU

# 776 TONGA

# 780 TRINIDAD AND TOBAGO

# 788 TUNISIA

# 949 TURKEY

# 934 TURKMENISTAN

# 840 TURKS AND CAICOS ISLANDS (THE)

# 036 TUVALU

# 800 UGANDA

# 980 UKRAINE

# 784 UNITED ARAB EMIRATES (THE)

# 826 UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (THE)

# 840 UNITED STATES MINOR OUTLYING ISLANDS (THE)

# 840 UNITED STATES OF AMERICA (THE)

# 997 UNITED STATES OF AMERICA (THE)

# 940 URUGUAY

# 858 URUGUAY

# 860 UZBEKISTAN

# 548 VANUATU

# 937 VENEZUELA (BOLIVARIAN REPUBLIC OF)

# 704 VIET NAM

# 840 VIRGIN ISLANDS (BRITISH)

# 840 VIRGIN ISLANDS (U.S.)

# 953 WALLIS AND FUTUNA

# 504 WESTERN SAHARA

# 886 YEMEN

# 967 ZAMBIA

# 932 ZIMBABWE

# : la #(입력)

That wasn't a number

# : 123123123 #(입력)

Choose a number from the list

# : 123123123 #(입력)

Choose a number from the list

# : 1231231231 #(입력)

Choose a number from the list

# : 123123123123 #(입력)

Choose a number from the list

# : 123123123 #(입력)

Choose a number from the list

# : lalalal #(입력)

That wasn't a number

# : 932 #(입력)

You choose ZIMBABWE

The currency code is Zimbabwe Dollar

# : lalalal #(입력)

That wasn't a number

# : 932 #(입력)

You choose ZIMBABWE

The currency code is Zimbabwe Dollar

# : 932 #(입력)

You choose ZIMBABWE

The currency code is Zimbabwe Dollar

# : 932 #(입력)

You choose ZIMBABWE

The currency code is Zimbabwe Dollar

2 comments