Write a program that asks the user to enter ten unique numbers. The program must add each number to a list if they aren’t already contained in the list. When the list contains ten unique numbers, the program displays the contents of the list and quits. Write a program that returns the number of strings where the first and last character in a string are the same, and the length of the string is 2 or longer. For example, if this is the list you use, ['bgh', 'wer', 'yuy', '1661'] the output should be 2 because the last 2 elements are longer than 1 char, and start and end with the same char. Hint: If you have a string such as word = “abc”, you can use word[-1] to access the last element in the string.
时间: 2024-02-02 09:04:18 浏览: 97
This program is to get the numbers of different characters
Sure, here are two Python programs that answer your questions:
Program 1: Adding Unique Numbers
```
numbers = []
while len(numbers) < 10:
new_num = int(input("Enter a unique number: "))
if new_num not in numbers:
numbers.append(new_num)
print(numbers)
```
This program uses a while loop to repeatedly ask the user for a number until the list contains ten unique numbers. If the user enters a number that is not already in the list, it is added to the list. Once the list contains ten unique numbers, the program prints the contents of the list and exits.
Program 2: Counting Strings with Same First and Last Character
```
strings = ['bgh', 'wer', 'yuy', '1661']
count = 0
for s in strings:
if len(s) >= 2 and s[0] == s[-1]:
count += 1
print(count)
```
This program uses a for loop to iterate through each string in the list. If the string has a length of 2 or more and its first and last characters are the same, the count variable is incremented. After all the strings have been checked, the program prints the final count. In this case, the output is 2 because only two strings in the list meet the conditions.
阅读全文